2017-08-10 66 views
0

我有3个运行的服务器,UI(或客户端),资源服务器和认证服务器。除了从资源服务器到客户端的信息之外,所有的事情都可以完美运行如果我使用邮递员或任何其他休息服务程序,我从认证服务器获取令牌,然后使用令牌从调查服务器获取信息。现在,我去我的客户端服务器,如果我不验证我重定向到我登录授权服务器,它使我回客户端服务器,它应该显示一个简单的一行:如何在春季启动时从客户端服务器获取资源服务器文本

灯都关了

但我只得到404空错误什么是应该表示它没有得到令牌与请求。

在我的资源服务器是一个简单的一行:

{“灯”:“灯都关了”}

任何帮助,如何摆脱资源服务器与信息赞赏附加到它的令牌。

我的资源服务器代码:

LightBoot.java 
@RestController public class LightsBoot { 
    private static final String lights = "lights are %s"; 
    @RequestMapping(value = "/lights") 
    public Lights greeting(@RequestParam(value = "name", defaultValue = "off") String name) { 
     return new Lights(String.format(lights, name)); 
    } } 

和我的客户端代码:

@SpringBootApplication public class ClientApplication { 

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

    @Bean 
    OAuth2RestTemplate oAuth2RestTemplate(OAuth2ClientContext clientContext, OAuth2ProtectedResourceDetails details) { 
     return new OAuth2RestTemplate(details, clientContext); 
    } 

    @Bean 
    public RestTemplate restTemplate() { 
     return new RestTemplate(); 
    } } 

@RestController public class NewController { 


    private RestTemplate restTemplate; 

    @Value("${endpoint.lights}") 
    private String lights; 


    @Autowired 
    public NewController(RestTemplate restTemplate) { 
     this.restTemplate = restTemplate; 
    } 


    public GetLights newLight() { 
     return restTemplate.getForObject(lights, GetLights.class); 
    } 

    @RequestMapping(value = "/") 
    public String lights() { 

     return newLight() + "  <--- here should be value of lights"; 
    } 

} 

客户端性能的样子:我的客户端应用程序的

auth-server: http://localhost:8081/authserver 
resource-server: http://localhost:8082/resource 
endpoint: 
    lights: http://localhost:8082/resource/lights 
server: 
    port: 8080 
security: 
    basic: 
    enabled: false 
    oauth2: 
    client: 
     client-id: user 
     client-secret: user-secret 
     access-token-uri: ${auth-server}/oauth/token 
     user-authorization-uri: ${auth-server}/oauth/authorize 
     scope: USER 
     authorized-grant-types: authorization_code 
    resource: 
     token-info-uri: ${auth-server}/oauth/check_token 
logging: 
    level: 
    org.springframework.security: DEBUG 

EDIT1 控制台输出

Redirecting to 'http://localhost:8081/authserver/oauth/authorize?client_id=jan&redirect_uri=http://localhost:8080/login&response_type=code&scope=USER&state=qJE3Rp' 
Retrieving token from http://localhost:8081/authserver/oauth/token 
2017-08-10 14:47:48.898 DEBUG 17560 --- [nio-8080-exec-2] g.c.AuthorizationCodeAccessTokenProvider : Encoding and sending form: {grant_type=[authorization_code], code=[0zeUdq], redirect_uri=[http://localhost:8080/login]} 
2017-08-10 14:47:48.928 DEBUG 17560 --- [nio-8080-exec-2] s.CompositeSessionAuthenticationStrategy : Delegating to org.springframework.security.w[email protected]327cec 
2017-08-10 14:47:48.929 DEBUG 17560 --- [nio-8080-exec-2] s.CompositeSessionAuthenticationStrategy : Delegating to o[email protected]1dd8d6 
2017-08-10 14:47:48.929 DEBUG 17560 --- [nio-8080-exec-2] uth2ClientAuthenticationProcessingFilter : Authentication success. Updating SecurityContextHolder to contain: or[email protected]e5d78cca: Principal: user; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=0:0:0:0:0:0:0:1, sessionId=<SESSION>, tokenType=bearertokenValue=<TOKEN>; Granted Authorities: ROLE_USER 
2017-08-10 14:47:48.929 DEBUG 17560 --- [nio-8080-exec-2] RequestAwareAuthenticationSuccessHandler : Redirecting to DefaultSavedRequest Url: http://localhost:8080/ 
2017-08-10 14:47:48.929 DEBUG 17560 --- [nio-8080-exec-2] o.s.s.web.DefaultRedirectStrategy  : Redirecting to 'http://localhost:8080/' 
+0

当你说“让我回到客户端服务器”时,你具体指什么?授权服务器是否返回重定向响应?如果是这样,那么重定向到哪里?这听起来像你在这一点上被发送到一个不正确的URL。 – DaveyDaveDave

+0

@DaveyDaveDave我添加了一个编辑,以显示什么控制台打印出来。要访问我的客户我去http:// localhost:8080,它需要我授权服务器,我登录,然后回到http:// localhost:8080 – Jan

回答

0

尝试在NewController加入@ResponseBody注释您lights()方法,所以它看起来像:

@RequestMapping(value = "/") 
@ResponseBody 
public String lights() { 
    return newLight() + "  <--- here should be value of lights"; 
} 

我怀疑正在发生的事情(尽管这主要是一种猜测,并会非常依赖在您的配置中)是由lights()方法返回的值,我认为是字符串lights are off <--- here should be value of lights,被Spring解释为视图名称,因此它正在寻找一个名为lights are off <--- here should be value of lights.html的文件,如src/main/resources/templates,这当然是不发现。

@ResponseBody注释告诉Spring只返回值作为响应的内容。

+0

saddly不,在resourceApp控制台我得到 访问是被拒绝(用户是匿名的);重定向到认证入口点和写入[错误=“未授权”,error_description =“完全认证是需要访问此资源”],因此它似乎并没有实际获取请求令牌。 – Jan

+0

我想我应该补充说客户端服务器正在使用Sso来运行,如果这有什么区别的话。我跟随的代码,在资源服务器中使用了h2数据库,然后运行。 – Jan

相关问题