gpt4 book ai didi

java - 身份验证不足异常 : Full authentication is required to access this resource

转载 作者:行者123 更新时间:2023-12-01 19:37:51 44 4
gpt4 key购买 nike

所以我有一个相对简单的 JWT 身份验证和 Spring boot 安全设置。这包括 3 个过滤器(Jwt、Cors、JwtExceptionHandler)和一个注册到 HttpSecurity 的异常处理程序。由于调试原因,我将 JwtRequestFilterdoFilterInternal 方法设为空。仅此一点不会造成任何问题。但是,如果同一过滤器中的 shouldNotFilter 方法返回 true (这意味着 doFilterInternal 将被跳过),我会收到指定的异常:

org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource
at org.springframework.security.web.access.ExceptionTranslationFilter.handleSpringSecurityException(ExceptionTranslationFilter.java:189)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:140)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at at.techsoft.cocos.security.ExceptionHandlerFilter.doFilterInternal(ExceptionHandlerFilter.java:20)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)

这是我的 WebSecurityConfig 类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);

@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

@Autowired
private UserDetailsService jwtUserDetailsService;

@Autowired
private ExceptionHandlerFilter exceptionHandlerFilter;

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

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}



@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.cors().and().csrf().disable();
httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint);

httpSecurity.headers().frameOptions().disable();

httpSecurity.authorizeRequests()
.antMatchers(Path.ROOT + "/authentication/**", "/*","/share/**", "/css/**", "/img/**", "/js/**", "/console/**").permitAll()
// Disallow everything else..
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated();

httpSecurity.addFilterBefore(new CorsFilterConfig(), ChannelProcessingFilter.class);

httpSecurity.addFilterBefore(new JwtRequestFilter(new StandardJwtValidator(tokenUtil, userRepository, jwtUserDetailsService)), UsernamePasswordAuthenticationFilter.class);

httpSecurity
.addFilterBefore(exceptionHandlerFilter, JwtRequestFilter.class);
}



@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(Path.ROOT + "/authentication/", "/*", "/css/**", "/img/**", "/js/**", "/console/**")
.antMatchers(HttpMethod.OPTIONS, "/**");

}

这是有问题的 JwtRequestFilter:

public class JwtRequestFilter extends OncePerRequestFilter {

private Logger log = LoggerFactory.getLogger(JwtRequestFilter.class);

final
IJwtValidator validator;

public JwtRequestFilter(IJwtValidator validator) {
this.validator = validator;
}

@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {

//SecurityContextHolder.getContext().setAuthentication(validator.getAuthenticatioNToken(request));


}

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return true;
}
}

当谷歌搜索异常时,几乎所有答案都包含 oauth2 身份验证,但我不使用 oauth2。

我应该补充一点,这种特殊情况永远不应该发生,但我觉得有一些潜在的问题正在系统的其他部分产生错误。

编辑:根据请求,我将添加 application.properties:

jwt.secret=javainuse
logging.level.at=DEBUG
logging.level.org.springframework.web=DEBUG
spring.jpa.hibernate.ddl-auto= create-drop
spring.jpa.open-in-view=true #i know this shouldn't be used

#database location
#left out for post
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2/
spring.datasource.url=#left out again

请求 header :

> GET /api/v1/groups/ HTTP/1.1
> Host: localhost:8080
> User-Agent: insomnia/7.0.5
> Cookie: JSESSIONID=199D19CCD05968D3E8CF0C97A1DE2CD5
> Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0c2FkbWluIiwiZXhwIjoxNTc1NTY0MjcxLCJpYXQiOjE1NzU1NDYyNzF9.dCXO2LFaZy8LTFRCA14gHAhA1kUUSy6pCZ7Joad2z1y1G50PSVC2mPz56odA5LmIHOxhjnZrrxAbGyuX2NWgWQ
> Accept: */*

最佳答案

您可以引用下面提到的文章,可能会有帮助

Spring Boot: Full authentication is required to access this resource

secure-your-spring-restful-apis-with-jwt-a-real-world-example

在您的 spring-boot 项目中,如果您有 application.properties 文件或 application.yml 文件,也可以共享该文件。 是服务器到服务器的身份验证还是客户端到服务器?请同时分享请求 header 详细信息

关于java - 身份验证不足异常 : Full authentication is required to access this resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195403/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com