├── .gitattributes ├── .gitignore ├── pom.xml └── src └── main ├── java └── com │ └── sprsec │ ├── controller │ ├── LinkNavigation.java │ └── SecurityNavigation.java │ ├── dao │ ├── RoleDAO.java │ ├── RoleDAOImpl.java │ ├── UserDAO.java │ └── UserDAOImpl.java │ ├── init │ ├── Initializer.java │ ├── RootConfig.java │ ├── SecurityConfig.java │ ├── SecurityInitializer.java │ └── WebAppConfig.java │ ├── model │ ├── Role.java │ └── User.java │ └── service │ ├── CustomUserDetailsService.java │ ├── RoleService.java │ ├── RoleServiceImpl.java │ ├── UserService.java │ └── UserServiceImpl.java ├── resources └── application.properties └── webapp ├── META-INF ├── MANIFEST.MF └── maven │ └── com │ └── security-spr │ ├── pom.properties │ └── pom.xml └── WEB-INF ├── pages ├── admin-first.jsp ├── admin-second.jsp ├── home.jsp ├── login-form.jsp ├── moderation.jsp └── success-login.jsp └── web.xml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com 5 | security-spr 6 | 7 | war 8 | 0.0.1-SNAPSHOT 9 | 10 | security-spr Maven Webapp 11 | 12 | 13 | 4.1.7.Final 14 | 5.1.21 15 | 1.7.5 16 | 3.2.4.RELEASE 17 | 3.2.0.RC1 18 | UTF-8 19 | 20 | 21 | 22 | 23 | 24 | org.hibernate 25 | hibernate-core 26 | ${hibernate.version} 27 | 28 | 29 | org.hibernate 30 | hibernate-entitymanager 31 | ${hibernate.version} 32 | 33 | 34 | 35 | mysql 36 | mysql-connector-java 37 | ${mysql.connector.version} 38 | 39 | 40 | commons-dbcp 41 | commons-dbcp 42 | 1.4 43 | 44 | 45 | javassist 46 | javassist 47 | 3.12.1.GA 48 | 49 | 50 | 51 | 52 | org.springframework 53 | spring-webmvc 54 | ${spring.version} 55 | 56 | 57 | commons-logging 58 | commons-logging 59 | 60 | 61 | 62 | 63 | org.springframework 64 | spring-tx 65 | ${spring.version} 66 | 67 | 68 | org.springframework 69 | spring-jdbc 70 | ${spring.version} 71 | 72 | 73 | org.springframework 74 | spring-orm 75 | ${spring.version} 76 | 77 | 78 | 79 | cglib 80 | cglib 81 | 2.2.2 82 | 83 | 84 | 85 | org.springframework.security 86 | spring-security-core 87 | ${spring.security.version} 88 | 89 | 90 | org.springframework.security 91 | spring-security-web 92 | ${spring.security.version} 93 | 94 | 95 | org.springframework.security 96 | spring-security-config 97 | ${spring.security.version} 98 | 99 | 100 | org.springframework.security 101 | spring-security-javaconfig 102 | 1.0.0.M1 103 | 104 | 105 | 106 | javax.servlet 107 | javax.servlet-api 108 | 3.0.1 109 | provided 110 | 111 | 112 | jstl 113 | jstl 114 | 1.2 115 | 116 | 117 | 118 | 119 | junit 120 | junit 121 | 4.7 122 | test 123 | 124 | 125 | 126 | 127 | 128 | repository.springsource.milestone 129 | SpringSource Milestone Repository 130 | http://repo.springsource.org/milestone 131 | 132 | 133 | 134 | 135 | security-spr 136 | 137 | 138 | maven-compiler-plugin 139 | 2.3.2 140 | 141 | 1.7 142 | 1.7 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/controller/LinkNavigation.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.servlet.ModelAndView; 7 | 8 | @Controller 9 | public class LinkNavigation { 10 | 11 | @RequestMapping(value="/", method=RequestMethod.GET) 12 | public ModelAndView homePage() { 13 | return new ModelAndView("home"); 14 | } 15 | 16 | @RequestMapping(value="/index", method=RequestMethod.GET) 17 | public ModelAndView indexPage() { 18 | return new ModelAndView("home"); 19 | } 20 | 21 | @RequestMapping(value="/sec/moderation", method=RequestMethod.GET) 22 | public ModelAndView moderatorPage() { 23 | return new ModelAndView("moderation"); 24 | } 25 | 26 | @RequestMapping(value="/admin/first", method=RequestMethod.GET) 27 | public ModelAndView firstAdminPage() { 28 | return new ModelAndView("admin-first"); 29 | } 30 | 31 | @RequestMapping(value="/admin/second", method=RequestMethod.GET) 32 | public ModelAndView secondAdminPage() { 33 | return new ModelAndView("admin-second"); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/controller/SecurityNavigation.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.servlet.ModelAndView; 7 | 8 | @Controller 9 | public class SecurityNavigation { 10 | 11 | @RequestMapping(value="/user-login", method=RequestMethod.GET) 12 | public ModelAndView loginForm() { 13 | return new ModelAndView("login-form"); 14 | } 15 | 16 | @RequestMapping(value="/error-login", method=RequestMethod.GET) 17 | public ModelAndView invalidLogin() { 18 | ModelAndView modelAndView = new ModelAndView("login-form"); 19 | modelAndView.addObject("error", true); 20 | return modelAndView; 21 | } 22 | 23 | @RequestMapping(value="/success-login", method=RequestMethod.GET) 24 | public ModelAndView successLogin() { 25 | return new ModelAndView("success-login"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/dao/RoleDAO.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.dao; 2 | 3 | import com.sprsec.model.Role; 4 | 5 | public interface RoleDAO { 6 | 7 | public Role getRole(int id); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/dao/RoleDAOImpl.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import org.hibernate.Session; 7 | import org.hibernate.SessionFactory; 8 | 9 | import com.sprsec.model.Role; 10 | 11 | @Repository 12 | public class RoleDAOImpl implements RoleDAO { 13 | 14 | @Autowired 15 | private SessionFactory sessionFactory; 16 | 17 | private Session getCurrentSession() { 18 | return sessionFactory.getCurrentSession(); 19 | } 20 | 21 | public Role getRole(int id) { 22 | Role role = (Role) getCurrentSession().load(Role.class, id); 23 | return role; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/dao/UserDAO.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.dao; 2 | 3 | import com.sprsec.model.User; 4 | 5 | public interface UserDAO { 6 | 7 | public User getUser(String login); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/dao/UserDAOImpl.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.dao; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.hibernate.Query; 8 | import org.hibernate.Session; 9 | import org.hibernate.SessionFactory; 10 | import org.springframework.stereotype.Repository; 11 | 12 | import com.sprsec.model.User; 13 | 14 | @Repository 15 | public class UserDAOImpl implements UserDAO { 16 | 17 | @Autowired 18 | private SessionFactory sessionFactory; 19 | 20 | private Session openSession() { 21 | return sessionFactory.getCurrentSession(); 22 | } 23 | 24 | public User getUser(String login) { 25 | List userList = new ArrayList(); 26 | Query query = openSession().createQuery("from User u where u.login = :login"); 27 | query.setParameter("login", login); 28 | userList = query.list(); 29 | if (userList.size() > 0) 30 | return userList.get(0); 31 | else 32 | return null; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/init/Initializer.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.init; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 5 | 6 | @Order(1) 7 | public class Initializer extends 8 | AbstractAnnotationConfigDispatcherServletInitializer { 9 | 10 | @Override 11 | protected Class[] getRootConfigClasses() { 12 | return new Class[] { RootConfig.class, SecurityConfig.class }; 13 | } 14 | 15 | @Override 16 | protected Class[] getServletConfigClasses() { 17 | return new Class[] { WebAppConfig.class }; 18 | } 19 | 20 | @Override 21 | protected String[] getServletMappings() { 22 | return new String[] { "/" }; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/init/RootConfig.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.init; 2 | 3 | import java.util.Properties; 4 | 5 | import javax.annotation.Resource; 6 | import javax.sql.DataSource; 7 | 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.ComponentScan; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.context.annotation.PropertySource; 12 | import org.springframework.core.env.Environment; 13 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 14 | import org.springframework.orm.hibernate4.HibernateTransactionManager; 15 | import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 16 | import org.springframework.transaction.annotation.EnableTransactionManagement; 17 | 18 | @Configuration 19 | @EnableTransactionManagement 20 | @ComponentScan("com.sprsec") 21 | @PropertySource("classpath:application.properties") 22 | public class RootConfig { 23 | 24 | private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 25 | private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 26 | private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 27 | private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 28 | 29 | private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 30 | private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 31 | private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan"; 32 | 33 | @Resource 34 | private Environment env; 35 | 36 | @Bean 37 | public DataSource dataSource() { 38 | DriverManagerDataSource dataSource = new DriverManagerDataSource(); 39 | 40 | dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 41 | dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 42 | dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 43 | dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 44 | 45 | return dataSource; 46 | } 47 | 48 | @Bean 49 | public LocalSessionFactoryBean sessionFactory() { 50 | LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 51 | sessionFactoryBean.setDataSource(dataSource()); 52 | sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 53 | sessionFactoryBean.setHibernateProperties(hibProperties()); 54 | return sessionFactoryBean; 55 | } 56 | 57 | private Properties hibProperties() { 58 | Properties properties = new Properties(); 59 | properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 60 | properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 61 | return properties; 62 | } 63 | 64 | @Bean 65 | public HibernateTransactionManager transactionManager() { 66 | HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 67 | transactionManager.setSessionFactory(sessionFactory().getObject()); 68 | return transactionManager; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/init/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.init; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 11 | 12 | import com.sprsec.service.CustomUserDetailsService; 13 | 14 | @Configuration 15 | @EnableWebSecurity 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Autowired 19 | private DataSource dataSource; 20 | 21 | @Autowired 22 | private CustomUserDetailsService customUserDetailsService; 23 | 24 | @Override 25 | protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { 26 | auth.jdbcAuthentication().dataSource(dataSource); 27 | } 28 | 29 | @Override 30 | protected void configure(HttpSecurity http) throws Exception { 31 | http.userDetailsService(customUserDetailsService) 32 | .authorizeRequests() 33 | .antMatchers("/sec/moderation.html").hasRole("MODERATOR") 34 | .antMatchers("/admin/**").hasRole("ADMIN") 35 | .and() 36 | .formLogin() 37 | .loginPage("/user-login.html") 38 | .defaultSuccessUrl("/success-login.html") 39 | .failureUrl("/error-login.html") 40 | .permitAll() 41 | .and() 42 | .logout() 43 | .logoutSuccessUrl("/index.html"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/init/SecurityInitializer.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.init; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 5 | 6 | @Order(2) 7 | public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/init/WebAppConfig.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.init; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 7 | import org.springframework.web.servlet.view.JstlView; 8 | import org.springframework.web.servlet.view.UrlBasedViewResolver; 9 | 10 | @Configuration 11 | @EnableWebMvc 12 | @ComponentScan("com.sprsec.controller") 13 | public class WebAppConfig { 14 | 15 | @Bean 16 | public UrlBasedViewResolver setupViewResolver() { 17 | UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 18 | resolver.setPrefix("/WEB-INF/pages/"); 19 | resolver.setSuffix(".jsp"); 20 | resolver.setViewClass(JstlView.class); 21 | return resolver; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.model; 2 | 3 | import java.util.Set; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.JoinTable; 11 | import javax.persistence.OneToMany; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name="roles") 16 | public class Role { 17 | 18 | @Id 19 | @GeneratedValue 20 | private Integer id; 21 | 22 | private String role; 23 | 24 | @OneToMany(cascade=CascadeType.ALL) 25 | @JoinTable(name="user_roles", 26 | joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}, 27 | inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")} 28 | ) 29 | private Set userRoles; 30 | 31 | public Integer getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public String getRole() { 40 | return role; 41 | } 42 | 43 | public void setRole(String role) { 44 | this.role = role; 45 | } 46 | 47 | public Set getUserRoles() { 48 | return userRoles; 49 | } 50 | 51 | public void setUserRoles(Set userRoles) { 52 | this.userRoles = userRoles; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/model/User.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.model; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | import javax.persistence.JoinColumn; 8 | import javax.persistence.JoinTable; 9 | import javax.persistence.OneToOne; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name="users") 14 | public class User { 15 | 16 | @Id 17 | @GeneratedValue 18 | private Integer id; 19 | 20 | private String login; 21 | 22 | private String password; 23 | 24 | @OneToOne(cascade=CascadeType.ALL) 25 | @JoinTable(name="user_roles", 26 | joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}, 27 | inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")} 28 | ) 29 | private Role role; 30 | 31 | public Integer getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public String getLogin() { 40 | return login; 41 | } 42 | 43 | public void setLogin(String login) { 44 | this.login = login; 45 | } 46 | 47 | public String getPassword() { 48 | return password; 49 | } 50 | 51 | public void setPassword(String password) { 52 | this.password = password; 53 | } 54 | 55 | public Role getRole() { 56 | return role; 57 | } 58 | 59 | public void setRole(Role role) { 60 | this.role = role; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/service/CustomUserDetailsService.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.security.core.GrantedAuthority; 9 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 10 | import org.springframework.security.core.userdetails.User; 11 | import org.springframework.security.core.userdetails.UserDetails; 12 | import org.springframework.security.core.userdetails.UserDetailsService; 13 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 14 | import org.springframework.stereotype.Service; 15 | import org.springframework.transaction.annotation.Transactional; 16 | 17 | import com.sprsec.dao.UserDAO; 18 | 19 | @Service 20 | @Transactional(readOnly=true) 21 | public class CustomUserDetailsService implements UserDetailsService { 22 | 23 | @Autowired 24 | private UserDAO userDAO; 25 | 26 | public UserDetails loadUserByUsername(String login) 27 | throws UsernameNotFoundException { 28 | 29 | com.sprsec.model.User domainUser = userDAO.getUser(login); 30 | 31 | boolean enabled = true; 32 | boolean accountNonExpired = true; 33 | boolean credentialsNonExpired = true; 34 | boolean accountNonLocked = true; 35 | 36 | return new User( 37 | domainUser.getLogin(), 38 | domainUser.getPassword(), 39 | enabled, 40 | accountNonExpired, 41 | credentialsNonExpired, 42 | accountNonLocked, 43 | getAuthorities(domainUser.getRole().getId()) 44 | ); 45 | } 46 | 47 | public Collection getAuthorities(Integer role) { 48 | List authList = getGrantedAuthorities(getRoles(role)); 49 | return authList; 50 | } 51 | 52 | public List getRoles(Integer role) { 53 | 54 | List roles = new ArrayList(); 55 | 56 | if (role.intValue() == 1) { 57 | roles.add("ROLE_MODERATOR"); 58 | roles.add("ROLE_ADMIN"); 59 | } else if (role.intValue() == 2) { 60 | roles.add("ROLE_MODERATOR"); 61 | } 62 | return roles; 63 | } 64 | 65 | public static List getGrantedAuthorities(List roles) { 66 | List authorities = new ArrayList(); 67 | 68 | for (String role : roles) { 69 | authorities.add(new SimpleGrantedAuthority(role)); 70 | } 71 | return authorities; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/service/RoleService.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.service; 2 | 3 | import com.sprsec.model.Role; 4 | 5 | public interface RoleService { 6 | 7 | public Role getRole(int id); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/service/RoleServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.transaction.annotation.Transactional; 6 | 7 | import com.sprsec.dao.RoleDAO; 8 | import com.sprsec.model.Role; 9 | 10 | @Service 11 | @Transactional 12 | public class RoleServiceImpl implements RoleService { 13 | 14 | @Autowired 15 | private RoleDAO roleDAO; 16 | 17 | public Role getRole(int id) { 18 | return roleDAO.getRole(id); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.service; 2 | 3 | import com.sprsec.model.User; 4 | 5 | public interface UserService { 6 | 7 | public User getUser(String login); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sprsec/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.sprsec.service; 2 | 3 | import org.springframework.transaction.annotation.Transactional; 4 | 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.sprsec.dao.UserDAO; 10 | import com.sprsec.model.User; 11 | 12 | @Service 13 | @Transactional 14 | public class UserServiceImpl implements UserService { 15 | 16 | @Autowired 17 | private UserDAO userDAO; 18 | 19 | public User getUser(String login) { 20 | return userDAO.getUser(login); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #DB properties: 2 | db.driver=com.mysql.jdbc.Driver 3 | db.url=jdbc:mysql://localhost:3306/hibnatedb 4 | db.username=hibuser 5 | db.password=root 6 | 7 | #Hibernate Configuration: 8 | hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect 9 | hibernate.show_sql=true 10 | entitymanager.packages.to.scan=com.sprsec.model -------------------------------------------------------------------------------- /src/main/webapp/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: Fruzenshtein 3 | Build-Jdk: 1.7.0_09 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /src/main/webapp/META-INF/maven/com/security-spr/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Tue Apr 30 17:02:56 EEST 2013 3 | version=0.0.1-SNAPSHOT 4 | groupId=com 5 | m2e.projectName=security-spr 6 | m2e.projectLocation=E\:\\spring_progs\\security-spr 7 | artifactId=security-spr 8 | -------------------------------------------------------------------------------- /src/main/webapp/META-INF/maven/com/security-spr/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com 5 | security-spr 6 | 7 | war 8 | 0.0.1-SNAPSHOT 9 | 10 | security-spr Maven Webapp 11 | 12 | 13 | 4.1.7.Final 14 | 5.1.21 15 | 1.6.6 16 | 3.1.3.RELEASE 17 | UTF-8 18 | 19 | 20 | 21 | 22 | 23 | org.hibernate 24 | hibernate-core 25 | ${hibernate.version} 26 | 27 | 28 | org.hibernate 29 | hibernate-entitymanager 30 | ${hibernate.version} 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | ${mysql.connector.version} 37 | 38 | 39 | commons-dbcp 40 | commons-dbcp 41 | 1.4 42 | 43 | 44 | javassist 45 | javassist 46 | 3.12.1.GA 47 | 48 | 49 | 50 | 51 | org.springframework 52 | spring-webmvc 53 | ${spring.version} 54 | 55 | 56 | org.springframework 57 | spring-tx 58 | ${spring.version} 59 | 60 | 61 | org.springframework 62 | spring-jdbc 63 | ${spring.version} 64 | 65 | 66 | org.springframework 67 | spring-orm 68 | ${spring.version} 69 | 70 | 71 | 72 | cglib 73 | cglib 74 | 2.2.2 75 | 76 | 77 | 78 | org.springframework.security 79 | spring-security-core 80 | ${spring.version} 81 | 82 | 83 | org.springframework.security 84 | spring-security-web 85 | ${spring.version} 86 | 87 | 88 | org.springframework.security 89 | spring-security-config 90 | ${spring.version} 91 | 92 | 93 | 94 | javax.servlet 95 | javax.servlet-api 96 | 3.0.1 97 | provided 98 | 99 | 100 | jstl 101 | jstl 102 | 1.2 103 | 104 | 105 | 106 | 107 | junit 108 | junit 109 | 4.7 110 | test 111 | 112 | 113 | 114 | 115 | security-spr 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/admin-first.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 | 5 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 6 | pageEncoding="ISO-8859-1"%> 7 | 8 | 9 | 10 | 11 | First Admin page 12 | 13 | 14 |

First Admin page

15 |

16 | " >Logout
17 | Home page
18 |

19 | 20 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/admin-second.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 | 5 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 6 | pageEncoding="ISO-8859-1"%> 7 | 8 | 9 | 10 | 11 | Second Admin page 12 | 13 | 14 |

Second Admin page

15 |

16 | " >Logout
17 | Home page
18 |

19 | 20 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/home.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 3 | pageEncoding="ISO-8859-1"%> 4 | 5 | 6 | 7 | 8 | Home page 9 | 10 | 11 |

Home page

12 |

This is Home page. It's available for all users.
13 | Moderation page
14 | First Admin page
15 | Second Admin page
16 |

17 | 18 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/login-form.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 | 5 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 6 | pageEncoding="ISO-8859-1"%> 7 | 8 | 9 | 10 | 11 | 12 | Login page 13 | 18 | 19 | 20 |

Login page

21 | 22 |

23 | 24 | Invalid login or password. 25 | 26 |

27 | 28 |
29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |
Login:
Password:
45 |
46 | 47 |

48 | Home page
49 |

50 | 51 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/moderation.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 | 5 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 6 | pageEncoding="ISO-8859-1"%> 7 | 8 | 9 | 10 | 11 | Moderation page 12 | 13 | 14 |

Moderation page

15 |

This is Moderation page. It's available for moderators.
16 | " >Logout
17 | Home page

18 | 19 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/pages/success-login.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 3 | pageEncoding="ISO-8859-1"%> 4 | 5 | 6 | 7 | 8 | Welcome page 9 | 10 | 11 |

Welcome page

12 |

You have successfully logged in.
13 | Home page

14 | 15 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | --------------------------------------------------------------------------------