Advertisement
plamen27

WebSecurityConfig JavaAdminBlog

Nov 25th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. package softuniBlog.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.autoconfigure.security.SecurityProperties;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.annotation.Order;
  7. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  8. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  12. import org.springframework.security.core.userdetails.UserDetailsService;
  13. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  14.  
  15. @Configuration
  16. @EnableGlobalMethodSecurity(prePostEnabled = true)
  17. @EnableWebSecurity
  18. @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  19. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  20. @Autowired
  21. private UserDetailsService userDetailsService;
  22.  
  23. @Autowired
  24. public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  25. auth.userDetailsService(this.userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
  26. }
  27.  
  28. @Override
  29. protected void configure(HttpSecurity http) throws Exception {
  30. http.authorizeRequests()
  31. .antMatchers("/admin/**").hasRole("ADMIN")
  32. .anyRequest().permitAll()
  33. .and()
  34. .formLogin().loginPage("/login")
  35. .usernameParameter("email").passwordParameter("password")
  36. .and()
  37. .logout().logoutSuccessUrl("/login?logout")
  38. .and()
  39. .exceptionHandling().accessDeniedPage("/error/403")
  40. .and()
  41. .csrf();
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement