【问题标题】:Spring WebSecurity configuration using .ignoring() not working使用 .ignoring() 的 Spring WebSecurity 配置不起作用
【发布时间】:2018-08-12 20:26:55
【问题描述】:

我已经为此苦苦挣扎了几天。除了通过简单地注释掉一些代码,运行程序,然后取消注释并再次运行来似乎不一致和不可预测地发生的古怪事情之外,我无法理解覆盖各种配置方法是如何工作的。

我希望 WebSecurity 始终忽略“/static/**”。

启动应用程序并导航到主页后,我可以访问我已允许访问的所有页面,但“/static/**”中的所有内容都将被忽略,直到我导航到登录页面并以经过身份验证的用户身份登录。所以应用程序只是显示为带有文本的白页,在登录之前根本没有任何样式。

这是我的 AppSecurityConfig 类的代码。我省略了处理登录成功和失败的辅助方法,而且我还要有不同的账户类型来服务不同的角色,所以为了简单起见,我这里只包括一个账户。我认为存在问题的部分是在我调用 .ignoring() 方法并传递“/static/**”参数的 configure(WebSecurity web) 方法中。提前致谢:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CompanyService companyService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(companyService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/",
                        "/account_registration",
                        "/candidate_registration",
                        "/addCandidate",
                        "/company_registration",
                        "/addCompany",
                        "/select_account_type",
                        "/candidate_login",
                        "/company_login").permitAll()
                .antMatchers("/company_profile").hasRole("COMPANY")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/company_login")
                .permitAll()
                .successHandler(companyLoginSuccessHandler())
                .failureHandler(companyLoginFailureHandler())
                .and()
            .logout()
                .logoutSuccessUrl("/");

    }
}

【问题讨论】:

标签: java spring spring-mvc spring-boot spring-security


【解决方案1】:

我的静态文件夹的路径是“src/main/resources/static”,但我按照 Sam 所说的做了,打开了开发者工具,发现“static”目录中的所有内容都被直接引用了。例如,有以这种方式引用的目录:“/vendor/...”和“/images/...”,它们被引用但由于安全原因被忽略。 “静态”目录中还有一些文件,例如“app.css”、“app.js”和“favicon.png”,它们的行为有些奇怪。看起来它们并没有被忽略,但是会显示不同的颜色和样式,除非我还将它们作为参数添加到 .gitIgnoring() 方法中,例如“/app.css”等。这个项目是通过完成 TeamTreehouse 教程和然后在我的 6 人团队中的几个人之间重构和添加自定义样式,我很确定在这个项目中继承了许多我和前端人员在样式方面不理解的东西。

虽然可能并不理想,但似乎可行的修复方法是从 .ignoring() 方法中删除“/static/**”,并将其替换为“static/”目录中实际存在的所有内容:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers( "/images/**",
        "/vendor/**",
        "/app.css",
        "/app.js",
        "/favicon.png");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2021-08-30
    • 2016-04-21
    相关资源
    最近更新 更多