You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
1.1 KiB
27 lines
1.1 KiB
package cc.bnblogs.springsecurity.config; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.http.HttpStatus; |
|
import org.springframework.security.core.AuthenticationException; |
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
|
import org.springframework.stereotype.Component; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.IOException; |
|
|
|
@Component |
|
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler { |
|
|
|
@Autowired |
|
private ObjectMapper mapper; |
|
|
|
@Override |
|
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, |
|
AuthenticationException exception) throws IOException { |
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); |
|
response.setContentType("application/json;charset=utf-8"); |
|
response.getWriter().write(mapper.writeValueAsString(exception.getMessage())); |
|
} |
|
} |