介绍
一周前,我开始使用OAuth2框架(使用Spring Boot v1.3.0.M4)开发应用程序.对我来说是一次全新的体验.所以我尽量让它变得更简单,以便更好地理解它.我正在使用Spring Security OAuth2,我正面临着正确使用它的困难.
What I want to do
当用户授权我的应用程序时,对用户进行身份验证.实际上,我不希望他在我的申请上注册,这样他就可以自由使用它,而无需填写无聊的表格进行注册.
Problem encountered
我找不到处理UserRedirectrequired Exception的方法.因为我没有这样做,所以用户永远不会被重定向到授权页面并抛出异常(并且未处理).
我的应用程序
StandardController.java
package org.test.oauth.web;import java.security.Principal;import org.springframework.web.bind.annotation.RequestMapPing;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class StandardController { @RequestMapPing(value = "/",method = RequestMethod.GET) public String getHelloWorld() { return "Hello world !"; } @RequestMapPing(value = "/user",method = RequestMethod.GET) public Principal getUser(Principal principal) { return principal; }}
StandardConfiguration.java
package org.test.oauth.configuration;import java.util.Arrays;import org.springframework.beans.factory.annotation.autowired;import org.springframework.boot.autoconfigure.security.oauth2.clIEnt.EnableOAuth2Sso;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.httpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.oauth2.clIEnt.oauth2clientContext;import org.springframework.security.oauth2.clIEnt.filter.oauth2clientContextFilter;import org.springframework.security.oauth2.clIEnt.resource.OAuth2ProtectedResourceDetails;import org.springframework.security.oauth2.clIEnt.token.grant.code.AuthorizationCodeResourceDetails;import org.springframework.security.web.access.ExceptionTranslationFilter;@Configuration@EnableOAuth2Ssopublic class StandardConfiguration extends WebSecurityConfigurerAdapter { @autowired private oauth2clientContextFilter oauth2clientContextFilter; @autowired private oauth2clientContext oauth2clientContext; @OverrIDe protected voID configure(httpSecurity http) throws Exception { // @formatter:off http .authorizeRequests().antMatchers("/login").anonymous().and() .authorizeRequests().anyRequest().authenticated().and() .httpBasic().and() .addFilterafter(oauth2clientContextFilter,ExceptionTranslationFilter.class); // @formatter:on }// org.springframework.beans.factory.NoUniqueBeanDeFinitionException: No qualifying bean of type [org.springframework.security.oauth2.clIEnt.OAuth2Restoperations] is defined: expected single matching bean but found 2: restTemplate,userInfoRestTemplate// @Bean// public OAuth2Restoperations restTemplate() {// return new oauth2resttemplate(bnetResource(),oauth2clientContext);// } @Bean public OAuth2ProtectedResourceDetails bnetResource() { AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails(); resource.setID("bnet"); resource.setClIEntID("***"); resource.setClIEntSecret("***"); resource.setAccesstokenUri("https://eu.battle.net/oauth/token"); resource.setUserAuthorizationUri("https://eu.battle.net/oauth/authorize"); resource.setScope(Arrays.asList("wow.profile")); return resource; }}
我的问题
当我使用我的root应用程序时,Spring Security会重定向我,因为我没有通过身份验证.它将我重定向到登录页面. Spring Boot默认配置抛出并处理了许多异常,但是当创建并抛出UserRedirectrequiredException时,没有过滤器处理它.调试我的应用程序,我发现我的oauth2clientContextFilter找到的最后一个异常是AccessDenIEdException.我怀疑我的过滤器(实际上是默认配置中的oauth2clientContextFilter)未在过滤器链中正确设置.
堆栈跟踪
org.springframework.security.oauth2.clIEnt.resource.UserRedirectrequiredException: A redirect is required to get the users approval at org.springframework.security.oauth2.clIEnt.token.grant.code.AuthorizationCodeAccesstokenProvIDer.getRedirectForAuthorization(AuthorizationCodeAccesstokenProvIDer.java:347) ~[spring-security-oauth2-2.0.7.RELEASE.jar:na] at org.springframework.security.oauth2.clIEnt.token.grant.code.AuthorizationCodeAccesstokenProvIDer.obtainAccesstoken(AuthorizationCodeAccesstokenProvIDer.java:194) ~[spring-security-oauth2-2.0.7.RELEASE.jar:na] at org.springframework.security.oauth2.clIEnt.oauth2resttemplate.acquireAccesstoken(oauth2resttemplate.java:221) ~[spring-security-oauth2-2.0.7.RELEASE.jar:na] at org.springframework.security.oauth2.clIEnt.oauth2resttemplate.getAccesstoken(oauth2resttemplate.java:173) ~[spring-security-oauth2-2.0.7.RELEASE.jar:na] at org.springframework.security.oauth2.clIEnt.filter.oauth2clientAuthenticationProcessingFilter.attemptAuthentication(oauth2clientAuthenticationProcessingFilter.java:94) ~[spring-security-oauth2-2.0.7.RELEASE.jar:na] at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:120) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:96) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.0.RELEASE.jar:4.2.0.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.header.headerWriterFilter.doFilterInternal(headerWriterFilter.java:64) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.0.RELEASE.jar:4.2.0.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.0.RELEASE.jar:4.2.0.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176) ~[spring-security-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.23.jar:8.0.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.23.jar:8.0.23] at org.springframework.web.filter.httpPutFormContentFilter.doFilterInternal(httpPutFormContentFilter.java:87) ~[spring-web-4.2.0.RELEASE.jar:4.2.0.RELEASE]
看看堆栈跟踪,我试图改变过滤器链中过滤器的顺序.所以我试图在oauth2clientAuthenticationProcessingFilter之后放置我的oauth2clientContextFilter.不幸的是,当我启动应用程序时,会发生错误,告诉我过滤器未注册.
更改
.addFilterafter(oauth2clientContextFilter,ExceptionTranslationFilter.class);
至
.addFilterafter(oauth2clientContextFilter,oauth2clientAuthenticationProcessingFilter.class);
堆栈跟踪
2015-08-25 12:05:50.990 ERROR 9132 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context: org.springframework.beans.factory.UnsatisfIEdDependencyException2015-08-25 12:05:51.054 WARN 9132 --- [ main] ationConfigEmbeddeDWebApplicationContext : Exception encountered during context initialization - cancelling refresh attemptjava.lang.IllegalArgumentException: Cannot register after unregistered Filter class org.springframework.security.oauth2.clIEnt.filter.oauth2clientAuthenticationProcessingFilter at org.springframework.security.config.annotation.web.builders.FilterComparator.registerafter(FilterComparator.java:145) ~[spring-security-config-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.springframework.security.config.annotation.web.builders.httpSecurity.addFilterafter(httpSecurity.java:960) ~[spring-security-config-4.0.2.RELEASE.jar:4.0.2.RELEASE] at org.test.oauth.configuration.StandardConfiguration.configure(StandardConfiguration.java:36) ~[classes/:na] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.gethttp(WebSecurityConfigurerAdapter.java:199) ~[spring-security-config-4.0.2.RELEASE.jar:4.0.2.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
所以我请你帮我解决这个问题并解决这个问题.我知道有很多关于这个问题的问题已经得到解答,但它无法帮助我.
感谢你花时间帮助我.
塞德里克
最佳答案@ Stilleur解决方案的替代方案如下.它在官方Spring指南的“处理重定向”标题下建议:https://spring.io/guides/tutorials/spring-boot-oauth2/@Beanpublic FilterRegistrationBean oauth2clientFilterRegistration( oauth2clientContextFilter filter) { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(filter); registration.setorder(-100); return registration;}
总结 以上是内存溢出为你收集整理的spring – 处理UserRedirectRequiredException(需要重定向才能获得用户批准)全部内容,希望文章能够帮你解决spring – 处理UserRedirectRequiredException(需要重定向才能获得用户批准)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)