Advertisement
Guest User

Untitled

a guest
Jul 7th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. @Service("userService")
  2. @Transactional(propagation = Propagation.REQUIRES_NEW )
  3. public class UserServiceImpl implements UserService, UserDetailsService{
  4.  
  5. @Autowired
  6. private UserDAO userDao;
  7.  
  8. @Override
  9. public boolean registerUser(User user) {
  10. User userWithTheSameLogin = userDao.getUserByLogin(user.getUserLogin());
  11. if(!Objects.isNull(userWithTheSameLogin)){
  12. //if user with the same login registered already
  13. return false;
  14. }else{
  15. User userWithTheSamePassword = userDao.getUserByEmail(user.getUserEmail());
  16. if(!Objects.isNull(userWithTheSamePassword)){
  17. //if user with the same email registered already
  18. return false;
  19. }
  20. else{
  21. //if user's credentials are unique
  22. userDao.create(user);
  23. return true;
  24. }
  25. }
  26.  
  27. }
  28.  
  29. @Override
  30. @Transactional(readOnly = true)
  31. public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  32. // TODO Auto-generated method stub
  33. User user = userDao.getUserByLogin(userName);
  34. if(user!=null){
  35. boolean enabled = user.getUserStatus().equals(UserStatus.ACTIVE);
  36. boolean accountNonExpired = user.getUserStatus().equals(UserStatus.ACTIVE);
  37. boolean credentialsNonExpired = user.getUserStatus().equals(UserStatus.ACTIVE);
  38. boolean accountNonLocked = user.getUserStatus().equals(UserStatus.ACTIVE);
  39.  
  40. Collection<GrantedAuthority> authorities = new ArrayList<>();
  41. authorities.add(new SimpleGrantedAuthority(user.getRole()));
  42.  
  43. org.springframework.security.core.userdetails.User securityUser =
  44. new org.springframework.security.core.userdetails.User(
  45. user.getUserLogin(), user.getUserPassword(), enabled,
  46. accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
  47. return securityUser;
  48. }else{
  49. throw new UsernameNotFoundException("Invalid user login");
  50. }
  51. }
  52. }
  53.  
  54. @Configuration
  55. @EnableWebSecurity
  56. @ComponentScan("org.dream.university.service")
  57. public class AppSecurityConfig{
  58.  
  59.  
  60. @Configuration
  61. @Order(1)
  62. public class AdminSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
  63.  
  64. @Override
  65. public void configure(WebSecurity web){
  66. web
  67. .ignoring()
  68. .antMatchers(new String[]{"/resources/**"});
  69. }
  70.  
  71. @Override
  72. protected void configure(HttpSecurity http) throws Exception {
  73. http
  74. .authorizeRequests()
  75. .antMatchers("/about","search*","/registration*","/login*","/bulbular*").permitAll()
  76. .antMatchers("/admin").hasRole("ADMIN")
  77. .anyRequest().authenticated()
  78. .and()
  79. .formLogin()
  80. .loginPage("/login")
  81. .permitAll()
  82. .defaultSuccessUrl("/students.html", true)
  83. .failureUrl("/login?error")
  84. .usernameParameter("username")
  85. .passwordParameter("password")
  86. .loginProcessingUrl("/j_spring_security_check")
  87. .and()
  88. .logout()
  89. .logoutUrl("/logout")
  90. .logoutSuccessUrl("/login")
  91. .and()
  92. .csrf()
  93. .disable();
  94.  
  95. }
  96.  
  97. @Bean(name = "authenticationManager")
  98. @Autowired
  99. public ProviderManager getProviderManager(DaoAuthenticationProvider daoAuthenticationProvider){
  100. List<AuthenticationProvider> providers = new ArrayList<>();
  101. providers.add(daoAuthenticationProvider);
  102. ProviderManager providerManager = new ProviderManager(providers);
  103. return providerManager;
  104.  
  105. }
  106.  
  107. @Bean
  108. @Autowired
  109. public DaoAuthenticationProvider daoAuthenticationProvider(UserDetailsService userDetailsService){
  110. DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
  111. provider.setUserDetailsService(userDetailsService);
  112. return provider;
  113. }
  114. }
  115. }
  116.  
  117. @Override
  118. @Transactional(readOnly = true)
  119. public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  120. // TODO Auto-generated method stub
  121. User user = userDao.getUserByLogin(userName);
  122. if(user.getRole().equals("ADMIN")||user==null)
  123. throw new UsernameNotFoundException("Invalid user login");
  124. }else return user;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement