Guest User

Untitled

a guest
Oct 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <form class="form" action="/j_spring_security_check" method="post">
  2. <div class="form-group">
  3. <label for="username" class="text-white">Username</label><br>
  4. <input type="text" name="username" id="username" class="form-control">
  5. </div>
  6. <div class="form-group">
  7. <label for="password" class="text-white">Password:</label><br>
  8. <input type="text" name="password" id="password" class="form-control">
  9. </div>
  10. <div class="form-group">
  11. <input type="submit" name="submit" class="btn btn-info btn-md" value="Enter">
  12. </div>
  13. </form>
  14.  
  15. @EnableWebSecurity
  16. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  17.  
  18. private final AdminDetailService adminDetailService;
  19.  
  20. public SecurityConfig(AdminDetailService adminDetailService) {
  21. this.adminDetailService = adminDetailService;
  22. }
  23.  
  24. @Override
  25. protected void configure(HttpSecurity http) throws Exception {
  26. http
  27. .antMatcher("/home/**")
  28. .authorizeRequests()
  29. .anyRequest()
  30. .hasRole("ADMIN")
  31. .and()
  32. .formLogin()
  33. .usernameParameter("username")
  34. .loginPage("/home/login").permitAll()
  35. .loginProcessingUrl("/j_spring_security_check")
  36. .defaultSuccessUrl("/home", true)
  37. .and()
  38. .logout()
  39. .logoutRequestMatcher(new AntPathRequestMatcher("/home/logout", "GET")).permitAll()
  40. .and()
  41. .rememberMe()
  42. .key("f@fdgdf543$cefdcF")
  43. .and().csrf().disable();
  44. }
  45.  
  46. @Override
  47. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  48. auth.userDetailsService(adminDetailService);
  49. }
  50.  
  51. }
  52.  
  53. @Service
  54. public class AdminDetailService implements UserDetailsService {
  55.  
  56. private final UserService userService;
  57.  
  58. public AdminDetailService(UserService userService) {
  59. this.userService = userService;
  60. }
  61.  
  62. @Override
  63. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  64. User user = userService.findByUsername(username);
  65. if (user == null) throw new UsernameNotFoundException(String.format("User with username [%s] not found.", username));
  66.  
  67. return new AuthUser(user.getLogin(), user.getPassword());
  68. }
  69. }
Add Comment
Please, Sign In to add comment