Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form class="form" action="/j_spring_security_check" method="post">
- <div class="form-group">
- <label for="username" class="text-white">Username</label><br>
- <input type="text" name="username" id="username" class="form-control">
- </div>
- <div class="form-group">
- <label for="password" class="text-white">Password:</label><br>
- <input type="text" name="password" id="password" class="form-control">
- </div>
- <div class="form-group">
- <input type="submit" name="submit" class="btn btn-info btn-md" value="Enter">
- </div>
- </form>
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- private final AdminDetailService adminDetailService;
- public SecurityConfig(AdminDetailService adminDetailService) {
- this.adminDetailService = adminDetailService;
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .antMatcher("/home/**")
- .authorizeRequests()
- .anyRequest()
- .hasRole("ADMIN")
- .and()
- .formLogin()
- .usernameParameter("username")
- .loginPage("/home/login").permitAll()
- .loginProcessingUrl("/j_spring_security_check")
- .defaultSuccessUrl("/home", true)
- .and()
- .logout()
- .logoutRequestMatcher(new AntPathRequestMatcher("/home/logout", "GET")).permitAll()
- .and()
- .rememberMe()
- .key("f@fdgdf543$cefdcF")
- .and().csrf().disable();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(adminDetailService);
- }
- }
- @Service
- public class AdminDetailService implements UserDetailsService {
- private final UserService userService;
- public AdminDetailService(UserService userService) {
- this.userService = userService;
- }
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- User user = userService.findByUsername(username);
- if (user == null) throw new UsernameNotFoundException(String.format("User with username [%s] not found.", username));
- return new AuthUser(user.getLogin(), user.getPassword());
- }
- }
Add Comment
Please, Sign In to add comment