AuthenticationSuccessHandler in Spring Security

前端 未结 2 726
無奈伤痛
無奈伤痛 2020-12-15 07:18

I\'ve used spring security in a Spring Boot application and there are 2 types of users: one is an ADMIN, and one just a simple user. I get the data from a DataSource

2条回答
  •  爱一瞬间的悲伤
    2020-12-15 08:03

    Rather than sublcassing AuthenticationSuccessHandler, It's worth knowing about the Spring security role-checking config:

    @Configuration
    @EnableWebSecurity
    public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
              .authorizeRequests()
              .antMatchers("/admin/**").hasRole("ADMIN");
        }
        ...
    } 
    

    OR pre-checking a Role per endpoint:

    @Autowired
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("/")
    public ModelAndView home(HttpServletRequest request) throws Exception {
    
    }
    

    where the default Role prefix is ROLE_

    https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html https://www.baeldung.com/spring-security-expressions-basic

提交回复
热议问题