2 回答

TA貢獻1805條經驗 獲得超9個贊
詳細說明 zfChaos 的答案,這是一個很好的線索,但沒有提供足夠的信息來使響應成為 JSON 響應:
您還應該設置內容類型和字符編碼。然后,編寫您的 JSON 響應(在本例中我使用了一個簡單的 String,當然使用類和 an 會更方便ObjectMapper)。
這是一個完整的例子:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.oauth2Login(login -> login
.failureHandler((request, response, exception) -> {
response.setContentType("application/json");
response.setStatus(401);
response.setCharacterEncoding("UTF-8");
response.getWriter().write("{ \"msg\": \"foo\" }");
})
);
}
}

TA貢獻1887條經驗 獲得超5個贊
將自定義添加AuthenticationFailureHandler到您的安全配置,然后在自定義實現中準備響應:
http.oauth2Login()
.failureHandler(customFailureHandler)
失敗處理程序示例:
public class CustomFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
response.sendError(401, "XML HERE");
}
}
- 2 回答
- 0 關注
- 161 瀏覽
添加回答
舉報