首页 > 解决方案 > LemonWebSecurityConfig 自定义

问题描述

我在我的项目中使用 SpringLemon。我想自定义authorizeRequests方法,以便任何以“/ xyz”开头的请求只能由经过身份验证的用户访问。(“/xyz/abc”、/xyz/def”、“xyz/ghi/jkl”等)为了做到这一点,我创建了自己的类扩展LemonWebSecurityConfig类,并将其作为配置类。我已经覆盖authorizeRequests方法看起来像这样:

@Override
    protected void authorizeRequests(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .mvcMatchers("/xyz/**").authenticated()
            .mvcMatchers("/**").permitAll();                  
    }

当我测试它时,它适用于那些“ /xyz ” URL(未经身份验证得到 403),“ /api/core/context ”给了我“200”,但“ /api/core/login ” URL 总是给了我 404 .即使我没有覆盖authorizeRequests方法并且我只有空的配置类,它也会响应 404。我错过了什么?

标签: spring-bootspring-lemon

解决方案


实际上我扩展了错误的课程。使用正确的类(如在 lemon-demo-jpa 中看到的那样)它可以完美地工作:

@Component
public class MySecurityConfig extends LemonJpaSecurityConfig {

    @Override
    protected void authorizeRequests(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .mvcMatchers("/xyz/**").authenticated();
        super.authorizeRequests(http);
    }
}

推荐阅读