我正在尝试通过手动添加所有依赖项等方式向自定义Java项目添加spring安全性。到目前为止,我已经成功了,但我(认为)我的WebSecurityConfigurerAdapter有一个问题:
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/index.html").hasAnyRole("USER", "ADMIN")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.formLogin()
.loginPage("/sign-in")
.permitAll();
}
当像上面那样限制index.html时,用户在输入应用程序基础URL(例如localhost:8080/myapp/)时会立即被要求登录。但是,如果我将antMatcher更改为:
...
http.authorizeRequests()
.antMatchers("/test**").hasAnyRole("USER", "ADMIN")
.and()
...
我不需要登录就可以访问应用程序的基本URL。值得一提的是,index.html和test.html是完全相同的(它们只包含一个h1标记),并且都位于生成的.war文件的根目录下:enter image description here
如何配置应用程序,使用户在输入基本url时不必登录,而只需在请求index.html时登录(例如,本地主机:8080/myapp/index.html)?
提前感谢
编辑:我的应用程序的端点位于localhost:8080/myapp/,如下所示:
@GetMapping("/")
public String home() {
return ("<h1>Welcome</h1>");
}
这个想法是,用户应该能够在不进行身份验证的情况下访问它。
发布于 2021-01-12 09:43:47
你可以试试这个。
'''
http.authorizeRequests()
.antMatchers("/test**").permitAll().antMatchers("/index.html").hasAnyRole("USER", "ADMIN")
.and()
'''
如果test.html是您的基本文件。
https://stackoverflow.com/questions/65675741
复制相似问题