Spring 安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/api/auth/**").permitAll()
.antMatchers("/api/test/**").permitAll()
.antMatchers("/").permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/static/**").permitAll()
.antMatchers("/manifest.json").permitAll()
.antMatchers("/logo192.png").permitAll()
.anyRequest().authenticated();
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
我也試過這個,但沒有產生任何結果
.antMatchers(HttpMethod.POST, "/api/auth/**").permitAll()
/api/auth/signup 回傳
error: "Unauthorized"
message: "Full authentication is required to access this resource"
path: "/error"
status: 401
Request URL: https://mysuite.ru/api/auth/signup
我該如何解決這個問題?
更新
@Configuration
public class MvcSecurityConfig implements WebMvcConfigurer {
@Value("${path.frontend}")
private String frontendPath;
@Value("${frontendStaticResourcesPathPatterns}")
private String[] frontendStaticResourcesPathPatterns;
private static final String BASE_API_PATH = "/";
public void addResourceHandlers(ResourceHandlerRegistry registry){
String pathToFrontend = "file:" this.frontendPath;
String pathToIndexHTML = pathToFrontend "/index.html";
registry
.addResourceHandler(frontendStaticResourcesPathPatterns)
.setCachePeriod(0)
.addResourceLocations(pathToFrontend);
registry.addResourceHandler("/", "/**")
.setCachePeriod(0)
.addResourceLocations(pathToIndexHTML)
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
if (resourcePath.startsWith(BASE_API_PATH) || resourcePath.startsWith(BASE_API_PATH.substring(1))) {
return null;
}
return location.exists() && location.isReadable() ? location : null;
}
});
}
}
這是我的 Spring MVC 配置。這是否會導致問題?我也試圖沿著路徑一步一步地做 permitAll 但它沒有用(api/,api/auth,api/autn/**)
uj5u.com熱心網友回復:
在 Ant 匹配器中,**匹配路徑中的零個或多個目錄。給定您的請求 URL,您只需要匹配零個或多個字符。話雖如此,請嘗試用以下內容替換您的 Ant 匹配器:
.antMatchers(HttpMethod.POST, "/api/auth/*").permitAll()
uj5u.com熱心網友回復:
通過過濾器,因為任何 API 請求都通過過濾器。您的 API 無法通過過濾器,因此您會收到 401 回應。
嘗試將其添加到您的 Spring Security 配置中:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/api/auth/**");
}
或者將其添加到 OncePerRequestFilter 中:
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return new AntPathMatcher().match("/api/auth/**", request.getServletPath());
}
uj5u.com熱心網友回復:
默認情況下,Spring Security啟用了CSRF 保護,因此當您執行不安全的請求(POST、PUT、DELETE)時,您必須提供CSRF 令牌。
在您的配置方法中,您可以禁用它以檢查它是否有效。
http.csrf().disable()
我建議您禁用 CSRF 保護可能對您的應用程式有害,您應該確定是否需要使用它。
此外,如果您使用的是 Spring Security 的 5.4 或更高版本,您可以啟用跟蹤日志來幫助您除錯它。
logging.level.org.springframework.security=TRACE
您可以在參考檔案中獲得更多詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328958.html
下一篇:帶括號的角度查詢引數?
