2 回答

TA貢獻1805條經(jīng)驗 獲得超9個贊
詳細說明 zfChaos 的答案,這是一個很好的線索,但沒有提供足夠的信息來使響應(yīng)成為 JSON 響應(yīng):
您還應(yīng)該設(shè)置內(nèi)容類型和字符編碼。然后,編寫您的 JSON 響應(yīng)(在本例中我使用了一個簡單的 String,當(dāng)然使用類和 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條經(jīng)驗 獲得超5個贊
將自定義添加AuthenticationFailureHandler到您的安全配置,然后在自定義實現(xiàn)中準備響應(yīng):
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 關(guān)注
- 153 瀏覽
添加回答
舉報