JSONログインペイロードを受け入れるようにSpring Securityを設定しようとしています。しかし、UsernamePasswordAuthenticationFilter
をカスタムフィルタとして使用した場合の副作用は、代わりにSpringセキュリティがAuthenticationSuccessHandler
の代わりに、ルートページ(デフォルトのスプリングセキュリティコンフィグレーション)へのスプリングリダイレクト要求に従わないことがあります。UsernamePasswordAuthenticationFilter AuthenticationSuccessHandlerを無視します。
注:私は設定からaddFilterAt
、削除してログインするFORM URL ENCODED
を使用する場合は、呼び出すことができます
WebSecurityConfigurer
http
.authorizeRequests()
.antMatchers(permitURI).permitAll()
.anyRequest().authenticated()
.and()
.addFilterAt(new JSONAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.formLogin().loginProcessingUrl("/login")
.successHandler(authSuccess).failureHandler(authFailure).permitAll()
.and()
.exceptionHandling().authenticationEntryPoint(authEntry)
.and()
.rememberMe().rememberMeCookieName("AUTOLOGIN")
.and()
.cors()
.and()
.logout().logoutUrl("/logout").permitAll()
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID", "AUTOLOGIN")
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.permitAll();
http.csrf().disable();
UsernamePasswordAuthenticationFilter
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (!request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
String payload;
try {
ObjectMapper mapper = new ObjectMapper();
payload = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
JsonAuthenticationParser auth = mapper.readValue(payload, JsonAuthenticationParser.class);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(auth.getUsername(), auth.getPassword());
return authRequest;
} catch (IOException e) {
throw new InternalAuthenticationServiceException(e.getMessage(), e);
}
}
static class JsonAuthenticationParser {
private final String username;
private final String password;
@JsonCreator
public JsonAuthenticationParser(@JsonProperty("username") String username, @JsonProperty("password") String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
AbstractAuthenticationProcessingFilter'は、メソッドのトンを実装する必要が '拡張、これを行うための簡単な方法はありますか?おそらく既存の 'successHandler'(' SimpleUrlAuthenticationSuccessHandler')などにリンクしていますか? – XPLOT1ON
UsernamePasswordAuthenticationFilterはすでにAbstractAuthenticationProcessingFilterを継承しています。 UsernamePasswordAuthenticationFilterを既に使用している場合、setAuthenticationSuccessHandlerをカスタムsuccessHandlerに呼び出すことができます。もちろん、必要に応じてSimpleUrlAuthenticationSuccessHandlerを使用することもできます。 – Persia
@ XPLOT1ON 'AbstractAuthenticationProcessingFilter'を拡張するには、あなたがすでに持っているメソッドを実装する必要があります。 –