gpt4 book ai didi

java - Spring Boot 。无法使用 token 进行身份验证

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:15 25 4
gpt4 key购买 nike

我正在实现OAuth2授权授予流程。通过登录页面登录时,用户能够接收访问 token 。但是,当我传递访问 token 来调用需要身份验证的端点时,它会触发异常。

以下是一些日志:

 |2017-08-05 22:37:54.102  INFO 18809 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter
chain:
org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration$NotOAuthRequestMatcher@7efa3f63,
[
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7134b8a7,
org.springframework.security.web.context.SecurityContextPersistenceFilter@3ff54f3d,
org.springframework.security.web.header.HeaderWriterFilter@7b61bf11,
org.springframework.security.web.authentication.logout.LogoutFilter@18b74ea,
org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter@1665fa54,
org.springframework.security.web.savedrequest.RequestCacheAwareFilter@14c93774,
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@62a68bcb,
org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2262d6d5,
org.springframework.security.web.session.SessionManagementFilter@40247d48,
org.springframework.security.web.access.ExceptionTranslationFilter@315105f,
org.springframework.security.web.access.intercept.FilterSecurityInterceptor@70025b99]

如您所见,链中有一个 OAuth2AuthenticationProcessingFilter

但是,当我调用端点时,我收到以下日志:

.808 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy        : /users at position 1 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2017-08-05 23:14:24.808 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 2 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2017-08-05 23:14:24.808 DEBUG 19570 --- [nio-8080-exec-3] w.c.HttpSessionSecurityContextRepository : No HttpSession currently exists
2017-08-05 23:14:24.808 DEBUG 19570 --- [nio-8080-exec-3] w.c.HttpSessionSecurityContextRepository : No SecurityContext was available from the HttpSession: null. A new one will be created.
2017-08-05 23:14:24.808 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 3 of 11 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2017-08-05 23:14:24.809 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@7091e577
2017-08-05 23:14:24.809 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 4 of 11 in additional filter chain; firing Filter: 'CsrfFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 5 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.u.matcher.AntPathRequestMatcher : Request 'GET /users' doesn't match 'POST /logout
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 6 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.u.matcher.AntPathRequestMatcher : Request 'GET /users' doesn't match 'POST /login
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 7 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 8 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.session.SessionManagementFilter : Requested session ID 82C8AE1B7613B93D9F52F5A09CA5D114 is invalid.
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2017-08-05 23:14:24.810 DEBUG 19570 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /users at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2017-08-05 23:14:24.811 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.u.matcher.AntPathRequestMatcher : Request 'GET /users' doesn't match 'POST /logout
2017-08-05 23:14:24.811 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/users'; against '/resources/**'
2017-08-05 23:14:24.811 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /users; Attributes: [authenticated]
2017-08-05 23:14:24.814 DEBUG 19570 --- [nio-8080-exec-3] o.s.b.a.audit.listener.AuditListener : AuditEvent [timestamp=Sat Aug 05 23:14:24 CEST 2017, principal=<unknown>, type=AUTHENTICATION_FAILURE, data={type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException, message=An Authentication object was not found in the SecurityContext}]
2017-08-05 23:14:24.817 DEBUG 19570 --- [nio-8080-exec-3] o.s.s.w.a.ExceptionTranslationFilter : Authentication exception occurred; redirecting to authentication entry point

正如您在第二个日志中看到的,它没有经过 OAuth2AuthenticationProcessingFilter

这是我的安全配置:

public class SecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and().anonymous().disable();
}
}

有人知道可能是什么原因吗?谢谢!

更新1

这是我的配置: https://gist.github.com/osgafarov/ef432de739f0e8dd2eb595c0c75aff1d

以下是我调用端点的方式:

curl -H "Authorization: bearer eaee916e-fdf1-4e80-808e-cfd9b2944539" localhost:8080/users

更新2

我发现如果我设置

security.oauth2.resource.filter-order = 3

然后上面的命令可以工作,但是使用此设置,当我调用 /oauth/authorize 时,我收到以下错误:“访问此资源需要完全身份验证。未经授权”。

最佳答案

我遇到了类似的问题,解决如下,需要配置资源服务器:

@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {

@Autowired
private TokenStore tokenStore;

@Override
public void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**", "/secured_area/**")
// etc. add more matchers
.authorizeRequests()
.anyRequest().authenticated();
}

@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore).resourceId("myapp");
}

}

此外,我相信从 Spring Boot 1.5.1+ 开始,安全过滤器顺序已更改,您可能想要覆盖该顺序:

 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { ..... }

关于java - Spring Boot 。无法使用 token 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526506/

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