Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Nov 10, 2023
1 parent 239b3ff commit 6c6c1be
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/spring/cs846bfb9a-7f69-11ee-b3f2-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.huifer.security.browser.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huifer.security.browser.support.Response;
import com.huifer.security.properties.LoginType;
import com.huifer.security.properties.SecurityProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* 描述:
*
* @author: huifer
* @date: 2019-11-17
*/
@Component
public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureHandler {
private Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private SecurityProperties securityProperties;
@Autowired
private ObjectMapper objectMapper;

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
log.info("登陆失败");

if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(new Response(exception.getMessage())));
} else {
super.onAuthenticationFailure(request, response, exception);
}

}
}

0 comments on commit 6c6c1be

Please sign in to comment.