【问题标题】:How to migrade Spring cloud security oauth2 from Angel.SR3 to Brixton.M3如何将 Spring cloud security oauth2 从 Angel.SR3 迁移到 Brixton.M3
【发布时间】:2016-02-28 19:47:19
【问题描述】:

我刚刚将 Spring Cloud 从 Angel.SR3 升级到 Brixton.M3,现在 Oauth2 安全部分已损坏。我已经被困了好几天了。

这里是 Angel.SR3 代码。

Application.java

package com.gaoshin;

import java.security.Principal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.security.oauth2.resource.EnableOAuth2Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableOAuth2Resource
@RestController
@RequestMapping(produces=MediaType.APPLICATION_JSON_VALUE)
public class Application {
    @RequestMapping(value="/", method = RequestMethod.GET)
    public String hi(Principal p) {
        return p!=null ? "Hello " + p.getName() : "Hello guest";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

资源服务器.java

package com.gaoshin;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
@EnableResourceServer 
public class ResourceServer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().cacheControl().disable();
        http.authorizeRequests().antMatchers("/**").permitAll();
    }

}

application.yml:

server:
    port: 9020

spring:
    oauth2:
        resource:
            userInfoUri: http://apie.pr720.com:8081/auth/user

一切正常。我可以使用下面的 curl 命令进行测试,它会返回正确的用户名。

curl "http://localhost:9020" -H "Authorization: Bearer 2d48412a-2e5a-425c-acbe-ab2ab31295dc" 

升级到 Brixton.M3 后,由于 EnableOAuth2Resource 不再存在,我将其替换为 EnableOAuth2Sso。同样的 curl 命令现在返回下面的错误,

{"timestamp":1448484209821,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"URI must not be null","path":"/"}

日志说:

java.lang.IllegalArgumentException: URI must not be null
at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.util.UriComponentsBuilder.fromUriString(UriComponentsBuilder.java:186) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.util.DefaultUriTemplateHandler.initUriComponentsBuilder(DefaultUriTemplateHandler.java:106) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.util.DefaultUriTemplateHandler.expand(DefaultUriTemplateHandler.java:100) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:556) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.oauth2.provider.token.RemoteTokenServices.postForMap(RemoteTokenServices.java:138) ~[spring-security-oauth2-2.0.8.RELEASE.jar:na]
at org.springframework.security.oauth2.provider.token.RemoteTokenServices.loadAuthentication(RemoteTokenServices.java:107) ~[spring-security-oauth2-2.0.8.RELEASE.jar:na]
at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager.authenticate(OAuth2AuthenticationManager.java:83) ~[spring-security-oauth2-2.0.8.RELEASE.jar:na]
at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:150) ~[spring-security-oauth2-2.0.8.RELEASE.jar:na]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]

【问题讨论】:

标签: spring-security-oauth2 spring-cloud


【解决方案1】:

正如 Dave 在https://github.com/spring-cloud/spring-cloud-security/issues/81 中提到的,配置是不同的。

配置前缀从 spring.oauth2.* 更改为 security.oauth2.*

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 2019-04-29
    • 2020-05-12
    • 1970-01-01
    • 2020-04-06
    • 2020-07-04
    • 2012-11-14
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多