ログイン/ログアウト用のRESTチャンネルを持つ片側アプリケーションを実装しました。私はJWTの実装から始めましたが、私はセッションベースの通信に移行したいと思います。私は現在、次のばねXML設定を有する:認証が存在する場合、それはログないようセッションで保存されていないRESTサービスによって作成されたSpringセキュリティトークン
<http pattern="/spring/login" security="none" create-session="always" />
<http pattern="/spring/logout" security="none" create-session="never" />
<http pattern="/spring/**" entry-point-ref="restAuthenticationEntryPoint"
create-session="always">
<csrf disabled="true" />
<custom-filter before="FORM_LOGIN_FILTER" ref="jwtAuthenticationFilter" />
</http>
次に、jwtAuthenticationFilter
はない場合、それはヘッダから解析され、修正される:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// skip if authorization already exists
Authentication sessionAuth = SecurityContextHolder.getContext().getAuthentication();
if (sessionAuth != null) {
LOGGER.info("Already authenticated as {}", sessionAuth.getPrincipal());
chain.doFilter(request, response);
return;
}
LOGGER.info("Try to authorize");
/スプリング/ loginメソッドは、トークンを作成し、セキュリティコンテキストに入れしようとします:
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS512), claimsSet);
try {
signedJWT.sign(signer);
String token = "Bearer " + signedJWT.serialize();
response.setToken(token);
Authentication auth = authenticationManager.authenticate(new JWTToken(signedJWT));
SecurityContextHolder.getContext().setAuthentication(auth);
} catch(JOSEException | ParseException e) {
私は何を期待、トークンは次のセキュリティコンテキストに存在するであろうということですREST呼び出し。しかし、ログは、次の要求によって、トークンがセキュリティコンテキスト内にないことを示しており、それが作成されてサーブレットフィルタによってそこに置かれ、その後の要求のために存在します。
私はここで間違っていますか?私は/ spring/login呼び出しで作成されるセッションを要求すると、SecurityContext
に入れられたAuthentication
も同様にセッションに保存されることを期待しています。それは明らかにここのケースではありません。 Spring SecurityがREST呼び出しに格納されたトークンを保持するためには、何が必要ですか?