【发布时间】: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("/");
}
}
【问题讨论】:
-
在浏览器中打开开发工具并检查被拒绝访问的资源的路径。它们可能与 /static/** 不同。
-
您也可以显示您的文件夹结构吗? “静态”文件夹在哪里?
-
如果您希望用户没有身份验证访问 /statics/** url,请删除 anyRequest().authenticated()。我认为这是覆盖忽略。
标签: java spring spring-mvc spring-boot spring-security