2017-10-23 6 views
0

私はSpring Securityを使用してSSO SAMLを実装しました。私のSpring Bootプロジェクトでは、基本的にユーザーをidPログインページにリダイレクトする次のコントローラを用意しています。ログインページに成功したら、JWTトークンを生成します。このJWTトークンは、ヘッダーとして索引ページに転送されます。しかし、私はこれが適切に機能するように思えない。認証後のSpringブートレンダリングインデックスページ

認証コントローラ、

@Controller 
public class AuthController { 

    private static final Logger log = LoggerFactory.getLogger(UserAccountResource.class); 

    @Inject 
    private TokenProvider tokenProvider; 

    /** 
    * Given that a user is already authenticated then generate a token 
    * 
    * @return the ResponseEntity with status 200 (OK) and with body of the updated {@link JWTToken} jwt token 
    */ 
    @RequestMapping(value = "/auth/login") 
    public String login(HttpServletResponse response) throws Exception { 
     final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if (authentication == null) { 
      throw new UnauthorizedException(); 
     } else { 
      try { 
       final SAMLCredential credential = (SAMLCredential) authentication.getCredentials(); 
       final DateTime dateTime = credential.getAuthenticationAssertion() 
         .getIssueInstant() 
         .toDateTime(DateTimeZone.forTimeZone(TimeZone.getDefault())); 
       String jwt = tokenProvider.createToken(authentication, dateTime.getMillis(), false); 
       response.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt); 
       log.debug("Generated jwt {}", jwt); 
       log.debug("SAMLCredential {}", credential); 
       return "forward:/"; 
      } catch (Exception e) { 
       throw new UnauthorizedException(e); 
      } 
     } 
    } 
} 

WebMvcConfigurerAdapter次のように限りSAML SSOを経由してすべてが素晴らしい作品になるよう、

@Configuration("webConfigurer") 
public class WebConfigurer extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("forward:/index.html"); 
    } 
} 

です。ユーザーはidPログインe.t.cにリダイレクトされます。私が理解できないのは、forwardが期待どおりに動作しない理由です。

すべて私のUI(角度4.x)はindex.htmlで開始されます。

これをテストしたところ、/に転送されていることがわかりましたが、ヘッダーは流れません。

答えて

0

私が最後にやったのは、ログインとjwtの生成を2つのAPI呼び出しに分けて、うまくいきました。

@GetMapping("/login") 
public String samlLogin() throws Exception { 
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
    if (authentication == null) { 
     throw new UnauthorizedException("Unable to get the SAML authentication information"); 
    } else { 
     try { 
      final SAMLCredential credential = (SAMLCredential) authentication.getCredentials(); 
      if (credential == null) { 
       throw new UnauthorizedException("Not valid SAML credentials"); 
      } 
      return "forward:/"; 
     } catch (Exception e) { 
      throw new UnauthorizedException(e); 
     } 
    } 
} 

@GetMapping("/jwt") 
public ResponseEntity<String> generateJWT(HttpServletResponse response) throws Exception { 
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
    if (authentication == null) { 
     throw new UnauthorizedException("Unable to get the SAML authentication information"); 
    } else { 
     try { 
      final SAMLCredential credential = (SAMLCredential) authentication.getCredentials(); 
      if (credential == null) { 
       throw new UnauthorizedException("Not valid SAML credentials"); 
      } 
      final DateTime dateTime = credential.getAuthenticationAssertion() 
        .getIssueInstant() 
        .toDateTime(DateTimeZone.forTimeZone(TimeZone.getDefault())); 
      String jwt = tokenProvider.createToken(authentication, dateTime.getMillis(), false); 
      response.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt); 
      log.debug("Generated jwt {} for SAML authentication", jwt); 
      return new ResponseEntity<>(jwt, HttpStatus.OK); 
     } catch (Exception e) { 
      throw new UnauthorizedException(e); 
     } 
    } 
} 
関連する問題