From 6b59d49dbcca1616d74c2814d87faf47e3aa7cf2 Mon Sep 17 00:00:00 2001 From: Sasha Sherstnev Date: Fri, 6 Dec 2013 18:13:14 +0000 Subject: [PATCH] partly working security: registration form, authorization, simple authentification --- .classpath | 5 +- WEB-INF/spring-security.xml | 42 ++++++++++++--- WEB-INF/web.xml | 25 +++++++++ conf/Proteocache.properties | 4 +- .../cassandra/CassandraNativeConnector.java | 24 ++++++++- .../controllers/DailyStatisticsController.java | 18 ++++--- server/compbio/controllers/HelpController.java | 20 ++++++-- server/compbio/controllers/IPDataController.java | 23 +++++---- .../controllers/JobExecutionTimeController.java | 11 ++-- server/compbio/controllers/JobLogController.java | 11 ++-- server/compbio/controllers/MainController.java | 54 ++++++++++++++++++-- server/compbio/controllers/SequenceController.java | 26 ++++++---- server/compbio/listeners/ContextListener.java | 2 - server/compbio/spring/WebConfig.java | 2 + webapp/resources/index.jsp | 5 +- webapp/view/fragments/mainmenu.jsp | 25 ++++++--- webapp/view/queryIP.jsp | 2 +- webapp/view/queryIPStatistics.jsp | 2 +- webapp/view/queryJobLog.jsp | 4 +- webapp/view/queryJobStatistics.jsp | 4 +- webapp/view/queryProteinSequence.jsp | 2 +- webapp/view/queryProteinSequenceCounter.jsp | 2 +- webapp/view/queryTimeExecution.jsp | 2 +- webapp/view/reportIP.jsp | 2 +- webapp/view/reportIPstatistics.jsp | 2 +- webapp/view/reportJobLog.jsp | 12 ++--- webapp/view/reportJobStatistics.jsp | 14 ++--- webapp/view/reportJobStatisticsOneDay.jsp | 8 +-- webapp/view/reportProteinSequences.jsp | 5 +- webapp/view/reportProteinSequencesCounter.jsp | 5 +- webapp/view/reportTimeExecution.jsp | 4 +- webapp/view/support/Notpermitted.jsp | 29 ----------- 32 files changed, 261 insertions(+), 135 deletions(-) delete mode 100644 webapp/view/support/Notpermitted.jsp diff --git a/.classpath b/.classpath index 2816de8..039628b 100644 --- a/.classpath +++ b/.classpath @@ -32,16 +32,17 @@ - + - + + diff --git a/WEB-INF/spring-security.xml b/WEB-INF/spring-security.xml index b5f0040..4e8b52c 100644 --- a/WEB-INF/spring-security.xml +++ b/WEB-INF/spring-security.xml @@ -1,4 +1,5 @@ - - - - + + + + + + + + + + + + + + + - + + diff --git a/WEB-INF/web.xml b/WEB-INF/web.xml index a3ec7a2..2032623 100644 --- a/WEB-INF/web.xml +++ b/WEB-INF/web.xml @@ -10,6 +10,31 @@ index.jsp + + + + + org.springframework.web.context.ContextLoaderListener + + + + contextConfigLocation + + /WEB-INF/spring-security.xml + + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + + springSecurityFilterChain + /* + + + proteocache org.springframework.web.servlet.DispatcherServlet diff --git a/conf/Proteocache.properties b/conf/Proteocache.properties index 2b2515a..b1730f4 100644 --- a/conf/Proteocache.properties +++ b/conf/Proteocache.properties @@ -1,8 +1,8 @@ ################################################################################# # Cassandra host or IP # test server is 10.0.115.190 -#cassandra.host=localhost -cassandra.host=gjb-www-1.cluster.lifesci.dundee.ac.uk +cassandra.host=localhost +#cassandra.host=gjb-www-1.cluster.lifesci.dundee.ac.uk cassandra.newtables.update=false diff --git a/datadb/compbio/cassandra/CassandraNativeConnector.java b/datadb/compbio/cassandra/CassandraNativeConnector.java index 0a5a795..5e5cf5a 100644 --- a/datadb/compbio/cassandra/CassandraNativeConnector.java +++ b/datadb/compbio/cassandra/CassandraNativeConnector.java @@ -3,6 +3,7 @@ package compbio.cassandra; import java.util.Calendar; import org.apache.log4j.Logger; +import org.springframework.dao.DataIntegrityViolationException; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Configuration; @@ -17,6 +18,8 @@ import com.datastax.driver.core.Row; import com.datastax.driver.core.SocketOptions; import com.datastax.driver.core.Session; +import com.datastax.driver.core.exceptions.QueryExecutionException; +import com.datastax.driver.core.exceptions.QueryValidationException; import com.datastax.driver.core.policies.Policies; import compbio.engine.ProteoCachePropertyHelperManager; @@ -82,14 +85,33 @@ public class CassandraNativeConnector { session.execute("CREATE TABLE IF NOT EXISTS JpredArchive " + "(JobID ascii, Protein varchar, IP ascii, StartTime bigint, ExecTime int, FinalStatus ascii, ExecutionStatus ascii, alignment map, " + "predictions map, ArchiveLink varchar, LOG varchar, PRIMARY KEY(JobID));"); - + session.execute("CREATE TABLE IF NOT EXISTS JobDateInfo " + "(jobday bigint, Total bigint, TotalOK bigint, TotalStopped bigint, TotalError bigint, TotalTimeOut bigint, Program varchar, Version varchar, PRIMARY KEY(jobday));"); + String com = "CREATE TABLE IF NOT EXISTS Users " + + "(name varchar, id bigint, email varchar, password varchar, organisation varchar, position varchar, signedtolist boolean, registrationdate bigint, PRIMARY KEY(id));"; + + + try { + session.execute(com); + } catch (QueryExecutionException e) { + String mess = "CassandraNativeConnector.CreateMainTables: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + } catch (QueryValidationException e) { + String mess = "CassandraNativeConnector.CreateMainTables: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + } + session.execute("CREATE INDEX IF NOT EXISTS ProteinSeq ON ProteinRow (protein);"); session.execute("CREATE INDEX IF NOT EXISTS ProteinIp ON ProteinLog (ip);"); session.execute("CREATE INDEX IF NOT EXISTS ON ProteinLog (ExecutionStatus);"); session.execute("CREATE INDEX IF NOT EXISTS ON FailLog (FinalStatus);"); + session.execute("CREATE INDEX IF NOT EXISTS ON Users (email);"); // session.execute("CREATE INDEX IF NOT EXISTS JobDateStamp ON ProteinData (jobtime);"); } diff --git a/server/compbio/controllers/DailyStatisticsController.java b/server/compbio/controllers/DailyStatisticsController.java index 142a298..d508c82 100644 --- a/server/compbio/controllers/DailyStatisticsController.java +++ b/server/compbio/controllers/DailyStatisticsController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -24,8 +25,9 @@ import compbio.statistic.StatisticsProt; @Controller public class DailyStatisticsController { - @RequestMapping(value = "/stat", method = RequestMethod.GET) - public String initFindForm(Map model) { + @RequestMapping(value = "/stat/jobs/query", method = RequestMethod.GET) + public String initFindForm(Map model, Principal principal) { + model.put("username", principal.getName()); Calendar cal = Calendar.getInstance(); String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE); cal.add(Calendar.DATE, -3); @@ -37,9 +39,10 @@ public class DailyStatisticsController { return "queryJobStatistics"; } - @RequestMapping(value = "/stat/query", method = RequestMethod.GET) + @RequestMapping(value = "/stat/jobsdaily/results", method = RequestMethod.GET) public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2, - @RequestParam("option") String option, Map model) { + @RequestParam("option") String option, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); @@ -59,9 +62,10 @@ public class DailyStatisticsController { return "/reportJobStatistics"; } - @RequestMapping(value = "/stat/oneday", method = RequestMethod.GET) - public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map model) throws ParseException { - + @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET) + public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map model, + Principal principal) throws ParseException { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); String realdate; diff --git a/server/compbio/controllers/HelpController.java b/server/compbio/controllers/HelpController.java index e0345b1..96ae042 100644 --- a/server/compbio/controllers/HelpController.java +++ b/server/compbio/controllers/HelpController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.util.Map; import org.springframework.stereotype.Controller; @@ -16,22 +17,33 @@ import org.springframework.web.bind.annotation.RequestMethod; public class HelpController { @RequestMapping(value = "/help/overview", method = RequestMethod.GET) - public String formOverviewPage(Map model) { + public String formOverviewPage(Map model, Principal principal) { + model.put("username", getPrincipleName(principal)); return "help/Overview"; } @RequestMapping(value = "/help/howto", method = RequestMethod.GET) - public String formHowtoPage(Map model) { + public String formHowtoPage(Map model, Principal principal) { + model.put("username", getPrincipleName(principal)); return "support/Notimplemented"; } @RequestMapping(value = "/help/doc", method = RequestMethod.GET) - public String formDocPage(Map model) { + public String formDocPage(Map model, Principal principal) { + model.put("username", getPrincipleName(principal)); return "support/Notimplemented"; } @RequestMapping(value = "/help/javadoc", method = RequestMethod.GET) - public String formJavadoc(Map model) { + public String formJavadoc(Map model, Principal principal) { + model.put("username", getPrincipleName(principal)); return "support/Notimplemented"; } + + private String getPrincipleName (Principal principal) { + if (null != principal.getName()) { + return principal.getName(); + } + return "unknown"; + } } diff --git a/server/compbio/controllers/IPDataController.java b/server/compbio/controllers/IPDataController.java index 424a742..690c57e 100644 --- a/server/compbio/controllers/IPDataController.java +++ b/server/compbio/controllers/IPDataController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.util.List; import java.util.Map; @@ -8,7 +9,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; -import compbio.cassandra.DataBase; import compbio.cassandra.TotalByCounterBean; import compbio.cassandra.UserBean; import compbio.statistic.CassandraRequester; @@ -20,21 +20,23 @@ import compbio.statistic.CassandraRequester; @Controller public class IPDataController { - @RequestMapping(value = "/ip/stat", method = RequestMethod.GET) - public String initStatisticsForm(Map model) { + @RequestMapping(value = "/admin/ip/counts/query", method = RequestMethod.GET) + public String initStatisticsForm(Map model, Principal principal) { + model.put("username", principal.getName()); model.put("value", 5); return "queryIPStatistics"; } - @RequestMapping(value = "/ip", method = RequestMethod.GET) - public String initOneIPForm(Map model) { + @RequestMapping(value = "/admin/ip/query", method = RequestMethod.GET) + public String initOneIPForm(Map model, Principal principal) { + model.put("username", principal.getName()); model.put("value", "127.0.0.1"); return "queryIP"; } - @RequestMapping(value = "/ip/stat/querycounter", method = RequestMethod.GET) - public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map model) { - + @RequestMapping(value = "/admin/ip/counts/results", method = RequestMethod.GET) + public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map model, Principal principal) { + model.put("username", principal.getName()); if (counter.equals("")) { model.put("error", "The value must not be empty"); model.put("value", counter); @@ -70,8 +72,9 @@ public class IPDataController { return "reportIPstatistics"; } - @RequestMapping(value = "/ip/query", method = RequestMethod.GET) - public String findIP(@RequestParam("ip") String ip, Map model) { + @RequestMapping(value = "/admin/ip/results", method = RequestMethod.GET) + public String findIP(@RequestParam("ip") String ip, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); UserBean r = cr.readIp(ip); diff --git a/server/compbio/controllers/JobExecutionTimeController.java b/server/compbio/controllers/JobExecutionTimeController.java index 6170078..71843a9 100644 --- a/server/compbio/controllers/JobExecutionTimeController.java +++ b/server/compbio/controllers/JobExecutionTimeController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.util.Calendar; import java.util.List; import java.util.Map; @@ -21,8 +22,9 @@ import compbio.statistic.StatisticsProt; @Controller public class JobExecutionTimeController { - @RequestMapping(value = "/exectime", method = RequestMethod.GET) - public String initFindForm(Map model) { + @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET) + public String initFindForm(Map model, Principal principal) { + model.put("username", principal.getName()); Calendar cal = Calendar.getInstance(); String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE); cal.add(Calendar.DATE, -3); @@ -33,9 +35,10 @@ public class JobExecutionTimeController { return "queryTimeExecution"; } - @RequestMapping(value = "/exectime/query", method = RequestMethod.GET) + @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET) public String findSequence(@RequestParam("date1") String date1, @RequestParam("date2") String date2, - @RequestParam(value="option", required=false) String option, Map model) { + @RequestParam(value="option", required=false) String option, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); CassandraRequester sp = new CassandraRequester(); diff --git a/server/compbio/controllers/JobLogController.java b/server/compbio/controllers/JobLogController.java index db6981d..28d6961 100644 --- a/server/compbio/controllers/JobLogController.java +++ b/server/compbio/controllers/JobLogController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.util.Map; import org.springframework.stereotype.Controller; @@ -16,14 +17,16 @@ import compbio.statistic.CassandraRequester; @Controller public class JobLogController { - @RequestMapping(value = "/joblog", method = RequestMethod.GET) - public String initFindForm(Map model) { + @RequestMapping(value = "/job/query", method = RequestMethod.GET) + public String initFindForm(Map model, Principal principal) { + model.put("username", principal.getName()); model.put("value", "jp_NzBOJKo"); return "queryJobLog"; } - @RequestMapping(value = "/joblog/query", method = RequestMethod.GET) - public String findJob(@RequestParam("IdJob") String jobid, Map model) { + @RequestMapping(value = "/job/results", method = RequestMethod.GET) + public String findJob(@RequestParam("IdJob") String jobid, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); CassandraRequester cr = new CassandraRequester(); model.put("result", cr.readJobLog(jobid)); diff --git a/server/compbio/controllers/MainController.java b/server/compbio/controllers/MainController.java index 7a8b926..f918c24 100644 --- a/server/compbio/controllers/MainController.java +++ b/server/compbio/controllers/MainController.java @@ -1,15 +1,59 @@ package compbio.controllers; +import java.security.Principal; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MainController { - @RequestMapping("home") - public String loadHomePage(Model m) { - m.addAttribute("name", "Proteocache"); + @RequestMapping(value = "/index", method = RequestMethod.GET) + public String printPublicHome(ModelMap model, Principal principal) { + String name = ""; + // if (null != name ) { + // name = principal.getName(); + // } + model.addAttribute("username", name); + model.addAttribute("message", "Spring Security Custom Form example"); + return "public"; + } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String login(ModelMap model) { + return "login"; + } + + @RequestMapping(value = "/loginfailed", method = RequestMethod.GET) + public String loginerror(ModelMap model) { + model.addAttribute("error", "Wrong user name or password"); + return "login"; + } + + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public String logout(ModelMap model) { + return "public"; + } + + @RequestMapping(value = "/denied", method = RequestMethod.GET) + public String denied(ModelMap model, Principal principal) { + String name = "unknown"; + if (null != principal.getName()) + name = principal.getName(); + model.put("username", name); + return "support/Denied"; + } + + @RequestMapping(value = "/home", method = RequestMethod.GET) + public String printHome(ModelMap model, Principal principal) { + String name = "unknown"; + if (null != name) { + name = principal.getName(); + } + model.addAttribute("username", name); + model.addAttribute("message", "Spring Security Custom Form example"); return "home"; } -} \ No newline at end of file + +} diff --git a/server/compbio/controllers/SequenceController.java b/server/compbio/controllers/SequenceController.java index 4bb9271..ee71258 100644 --- a/server/compbio/controllers/SequenceController.java +++ b/server/compbio/controllers/SequenceController.java @@ -1,5 +1,6 @@ package compbio.controllers; +import java.security.Principal; import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -10,13 +11,11 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import compbio.cassandra.ProteinBean; -import compbio.cassandra.DataBase; -import compbio.data.sequence.SequenceUtil; import compbio.cassandra.TotalByCounterBean; import compbio.statistic.CassandraRequester; /** - * Spring controller for sequence search. This version works in the servlet + * Spring controller for sequence queries. This version works in the servlet * style. * * @author Alexander Sherstnev @@ -26,6 +25,7 @@ import compbio.statistic.CassandraRequester; * @version 1.0 December 2013 */ @Controller +@RequestMapping("/sequence") public class SequenceController { /** @@ -33,20 +33,23 @@ public class SequenceController { */ private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE); - @RequestMapping(value = "/sequence/query1", method = RequestMethod.GET) - public String formSequenceQuery(Map model) { + @RequestMapping(value = "query", method = RequestMethod.GET) + public String formSequenceQuery(Map model, Principal principal) { + model.put("username", principal.getName()); model.put("value", "AAAAA"); return "queryProteinSequence"; } - @RequestMapping(value = "/sequence/query2", method = RequestMethod.GET) - public String formCounterQuery(Map model) { + @RequestMapping(value = "counts/query", method = RequestMethod.GET) + public String formCounterQuery(Map model, Principal principal) { + model.put("username", principal.getName()); model.put("value", 5); return "queryProteinSequenceCounter"; } - @RequestMapping(value = "/sequence/querysequence", method = RequestMethod.GET) - public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("protein") String flag, Map model) { + @RequestMapping(value = "sequence/results", method = RequestMethod.GET) + public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("protein") String flag, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); // input checks @@ -82,8 +85,9 @@ public class SequenceController { return "reportProteinSequences"; } - @RequestMapping(value = "/sequence/querycounter", method = RequestMethod.GET) - public String countSequences(@RequestParam("counterJob") String counter, Map model) { + @RequestMapping(value = "counts/results", method = RequestMethod.GET) + public String countSequences(@RequestParam("counterJob") String counter, Map model, Principal principal) { + model.put("username", principal.getName()); final long startTime = System.currentTimeMillis(); if (counter.equals("")) { diff --git a/server/compbio/listeners/ContextListener.java b/server/compbio/listeners/ContextListener.java index aa2fa24..c3109a7 100644 --- a/server/compbio/listeners/ContextListener.java +++ b/server/compbio/listeners/ContextListener.java @@ -11,8 +11,6 @@ import javax.servlet.annotation.WebListener; import org.apache.log4j.Logger; -import com.datastax.driver.core.Session; - import compbio.cassandra.CassandraNativeConnector; import compbio.cassandra.CassandraNewTableWriter; import compbio.cassandra.JpredParserHTTP; diff --git a/server/compbio/spring/WebConfig.java b/server/compbio/spring/WebConfig.java index e7f0be2..0a0de7a 100644 --- a/server/compbio/spring/WebConfig.java +++ b/server/compbio/spring/WebConfig.java @@ -3,6 +3,7 @@ package compbio.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -10,6 +11,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc +@ImportResource("WEB-INF/spring-security.xml") @ComponentScan(basePackages = "compbio.controllers") public class WebConfig extends WebMvcConfigurerAdapter { diff --git a/webapp/resources/index.jsp b/webapp/resources/index.jsp index ec49fb6..97584c7 100644 --- a/webapp/resources/index.jsp +++ b/webapp/resources/index.jsp @@ -2,10 +2,9 @@ - + - If you are not redirected automatically, click - here. + If you are not redirected automatically, click here. diff --git a/webapp/view/fragments/mainmenu.jsp b/webapp/view/fragments/mainmenu.jsp index ed67bb7..1379729 100644 --- a/webapp/view/fragments/mainmenu.jsp +++ b/webapp/view/fragments/mainmenu.jsp @@ -1,9 +1,10 @@ <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> -