2017-10-31 6 views
1

OAuth 2.0 및 Authorization Server를 사용하는 Spring Boot 앱이 있습니다. 보안 페이지에 액세스하려고하면 인증 서버 (Blitz Identity Provider)의 로그인 페이지에서 리디렉션되고 모든 것이 제대로 작동해야합니다. 내 문제는 그 것이다 @Controller (보안 페이지에)의 승인 토큰을 추출 할 수 없습니다. 나중에 두 번째 응용 프로그램에서 권한을 부여하기 위해 사용하려는 토큰.@Controller에서 인증 토큰을 추출하는 방법

  • 시도 (대답) this thing하고 일, 나는 다시 내 토큰을 가지고, 그러나 당신이 볼 수 있듯이, 그것은 사용자 이름과 암호 매개 변수의 하드 코드이고이 로그인을 통해 로그인 같다 - 난 몰라 두 번째로 에 로그인해야합니다 (인증 된 페이지에서).
  • 출력에 authentication.getDetails을 시도(), 그것은 토큰 유형 하고 < TOKEN> 같은 토큰을 보여 주지만 충분하지 않습니다.
  • 요청 - 응답 헤더에서 토큰 조회를 시도했지만 찾지 못 했으므로 권한 서버가 헤더로 보내지 않습니다.

여기 내 컨텍스트의 일부를 이해하는 데 도움이되는 두 개의 파일이 있습니다.

application.yml

server: 
    port: 8080 
    context-path:/
    session: 
    cookie: 
     name:FIRSTSESSION 
security: 
    basic: 
    enabled: false 
    oauth2: 
    client: 
     clientId: test_id 
     clientSecret: f3M5m9a2Dn0v15l 
     accessTokenUri: http://server:9000/blitz/oauth/te 
     userAuthorizationUri: http://server:9000/blitz/oauth/ae?scope=test_scope 
    resource: 
     userInfoUri: http://server:9000/blitz/oauth/me 
logging: 
    level: 
    org.springframework.security: DEBUG 

SsoController.java는

@EnableOAuth2Sso 
@Controller 
public class SsoController { 

    @RequestMapping("/secondService") 
    public String getContent(HttpServletRequest request, Model model) { 

     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     model.addAttribute("submittedValue", authentication.getDetails()); 
     return "secondService"; 
    } 
} 

그래서, 당신은 무엇을 제안 할 수 있습니다? 이 경우 인증 토큰을 어떻게 추출 할 수 있습니까?

답변

1

구성한 경우 OAuth2를 인증 당신이 코드 아래에 시도 할 수/리소스 서버 :

@Autowired 
private TokenStore tokenStore; 

@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET}, value = "/oauth/me") 
public Map<String, Object> userInfo(OAuth2Authentication auth){ 
    final OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) auth.getDetails(); 
    //token 
    String accessToken = details.getTokenValue(); 
    //reference 
    final OAuth2AccessToken accessToken = tokenStore.readAccessToken(details.getTokenValue()); 
    // clientid 
    String clientId = auth.getOAuth2Request().getClientId(); 
} 

는 희망이 도움이!

+0

예! 그게 바로 내가 필요로하는 것입니다! 이 코드를 구현하여 토큰을 얻었습니다. OAuth2AuthenticationDetails = auth = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext(). getAuthentication(). getDetails(); accessToken = auth.getTokenValue(); – Artemoon