Advertisement
ImHungryHi

securityconfig

Sep 17th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. package cc.serviceops.config;
  2. //import cc.serviceops.account.UserDetailsServiceImpl;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.access.expression.SecurityExpressionHandler;
  7. import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
  8. import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
  9. import org.springframework.security.authentication.AuthenticationManager;
  10. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  11. import org.springframework.security.config.BeanIds;
  12. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  13. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  14. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  15. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  16. import org.springframework.security.core.userdetails.UserDetailsService;
  17. import org.springframework.security.crypto.factory.PasswordEncoderFactories;
  18. import org.springframework.security.crypto.password.PasswordEncoder;
  19. import org.springframework.security.web.FilterInvocation;
  20. import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
  21. import javax.sql.DataSource;
  22.  
  23. @Configuration
  24. @EnableWebSecurity
  25. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  26. private DataSource dataSource;
  27.  
  28. @Autowired
  29. public void setDataSource(DataSource dataSource){
  30. this.dataSource = dataSource;
  31. }
  32. //@Bean
  33. //public UserDetailsService userDetailsService() {
  34. //return new UserDetailsServiceImpl();
  35. //}
  36.  
  37. @Bean
  38. public PasswordEncoder encoder() {
  39. return PasswordEncoderFactories.createDelegatingPasswordEncoder();
  40. }
  41.  
  42. @Bean
  43. public DaoAuthenticationProvider authenticationProvider() {
  44. DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
  45. authenticationProvider.setUserDetailsService(userDetailsService());
  46. authenticationProvider.setPasswordEncoder(encoder());
  47. return authenticationProvider;
  48. }
  49.  
  50. // ========= Custom login and permissions ========= //
  51. @Override
  52. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  53. auth
  54. .jdbcAuthentication()
  55. .dataSource(dataSource)
  56. .usersByUsernameQuery("select email,password,enabled from account where email = ?")
  57. .authoritiesByUsernameQuery("select email,role from account where email = ?");
  58. // .authenticationProvider(authenticationProvider());
  59. }
  60.  
  61. @Override
  62. protected void configure(HttpSecurity http) throws Exception {
  63. http
  64. .authorizeRequests()
  65. .expressionHandler(webExpressionHandler())
  66. .antMatchers("/", "/home", "/register", "/css/**", "/img/**", "/scripts/**", "/fonts/**", "/api/organisations**").permitAll()
  67. .antMatchers("/signup", "/**", "/**/**").permitAll()
  68. .antMatchers("/dashboard").hasAnyRole("AGENT", "ADMIN")
  69. .antMatchers("/organisation", "/categories", "/teams", "/users").hasRole("ADMIN")
  70. .anyRequest().authenticated()
  71. .and()
  72. .formLogin()
  73. .loginPage("/login")
  74. .defaultSuccessUrl("/dashboard", true)
  75. .permitAll()
  76. .and()
  77. .logout()
  78. .logoutSuccessUrl("/login?logout")
  79. .permitAll()
  80. .and()
  81. .rememberMe().key("uniqueAndSecret");
  82. }
  83.  
  84. @Bean
  85. public PasswordEncoder passwordEncoder() {
  86. return PasswordEncoderFactories.createDelegatingPasswordEncoder();
  87. }
  88.  
  89. @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
  90. @Override
  91. public AuthenticationManager authenticationManagerBean() throws Exception {
  92. return super.authenticationManagerBean();
  93. }
  94.  
  95. @Bean
  96. public RoleHierarchy roleHierarchy() {
  97. RoleHierarchyImpl hierarchy = new RoleHierarchyImpl();
  98. hierarchy.setHierarchy("ADMIN > AGENT\nAGENT > GUEST");
  99. return hierarchy;
  100. }
  101.  
  102. private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
  103. DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
  104. defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy());
  105. return defaultWebSecurityExpressionHandler;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement