Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cc.serviceops.config;
- //import cc.serviceops.account.UserDetailsServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.access.expression.SecurityExpressionHandler;
- import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
- import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
- import org.springframework.security.config.BeanIds;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.crypto.factory.PasswordEncoderFactories;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.security.web.FilterInvocation;
- import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
- import javax.sql.DataSource;
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- private DataSource dataSource;
- @Autowired
- public void setDataSource(DataSource dataSource){
- this.dataSource = dataSource;
- }
- //@Bean
- //public UserDetailsService userDetailsService() {
- //return new UserDetailsServiceImpl();
- //}
- @Bean
- public PasswordEncoder encoder() {
- return PasswordEncoderFactories.createDelegatingPasswordEncoder();
- }
- @Bean
- public DaoAuthenticationProvider authenticationProvider() {
- DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
- authenticationProvider.setUserDetailsService(userDetailsService());
- authenticationProvider.setPasswordEncoder(encoder());
- return authenticationProvider;
- }
- // ========= Custom login and permissions ========= //
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth
- .jdbcAuthentication()
- .dataSource(dataSource)
- .usersByUsernameQuery("select email,password,enabled from account where email = ?")
- .authoritiesByUsernameQuery("select email,role from account where email = ?");
- // .authenticationProvider(authenticationProvider());
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .expressionHandler(webExpressionHandler())
- .antMatchers("/", "/home", "/register", "/css/**", "/img/**", "/scripts/**", "/fonts/**", "/api/organisations**").permitAll()
- .antMatchers("/signup", "/**", "/**/**").permitAll()
- .antMatchers("/dashboard").hasAnyRole("AGENT", "ADMIN")
- .antMatchers("/organisation", "/categories", "/teams", "/users").hasRole("ADMIN")
- .anyRequest().authenticated()
- .and()
- .formLogin()
- .loginPage("/login")
- .defaultSuccessUrl("/dashboard", true)
- .permitAll()
- .and()
- .logout()
- .logoutSuccessUrl("/login?logout")
- .permitAll()
- .and()
- .rememberMe().key("uniqueAndSecret");
- }
- @Bean
- public PasswordEncoder passwordEncoder() {
- return PasswordEncoderFactories.createDelegatingPasswordEncoder();
- }
- @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
- @Override
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Bean
- public RoleHierarchy roleHierarchy() {
- RoleHierarchyImpl hierarchy = new RoleHierarchyImpl();
- hierarchy.setHierarchy("ADMIN > AGENT\nAGENT > GUEST");
- return hierarchy;
- }
- private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
- DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
- defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy());
- return defaultWebSecurityExpressionHandler;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement