parent
c9a4b9c5a1
commit
3bdbd9d2df
5 changed files with 152 additions and 3 deletions
@ -0,0 +1,35 @@ |
|||||||
|
package cc.bnblogs.springinit.config; |
||||||
|
|
||||||
|
import cc.bnblogs.springinit.utils.AjaxResponse; |
||||||
|
import cc.bnblogs.springinit.utils.GlobalConfig; |
||||||
|
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("loginFailureHandler") |
||||||
|
public class LoginFailureHandler implements AuthenticationFailureHandler{ |
||||||
|
@Autowired |
||||||
|
private ObjectMapper objectMapper; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException { |
||||||
|
httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); |
||||||
|
httpServletResponse.setContentType("application/json;charset=UTF-8"); |
||||||
|
|
||||||
|
httpServletResponse.getWriter().write(objectMapper.writeValueAsString( |
||||||
|
AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(), |
||||||
|
GlobalConfig.ResponseCode.ERROR.getDesc(), |
||||||
|
"登录失败:"+e.getMessage() |
||||||
|
))); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,31 @@ |
|||||||
|
package cc.bnblogs.springinit.config; |
||||||
|
|
||||||
|
import cc.bnblogs.springinit.utils.AjaxResponse; |
||||||
|
import cc.bnblogs.springinit.utils.GlobalConfig; |
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.security.core.Authentication; |
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import javax.servlet.ServletException; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
@Component("loginSuccessHandler") |
||||||
|
public class LoginSuccessHandler implements AuthenticationSuccessHandler { |
||||||
|
@Autowired |
||||||
|
private ObjectMapper objectMapper; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { |
||||||
|
httpServletResponse.setContentType("application/json;charset=UTF-8"); |
||||||
|
httpServletResponse.getWriter().write(objectMapper.writeValueAsString( |
||||||
|
AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(), |
||||||
|
GlobalConfig.ResponseCode.SUCCESS.getDesc(), |
||||||
|
authentication.getName() |
||||||
|
))); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,42 @@ |
|||||||
|
package cc.bnblogs.springinit.utils; |
||||||
|
|
||||||
|
|
||||||
|
public class AjaxResponse { |
||||||
|
private Integer status; |
||||||
|
private String msg; |
||||||
|
private Object obj; |
||||||
|
|
||||||
|
public Integer getStatus() { |
||||||
|
return status; |
||||||
|
} |
||||||
|
|
||||||
|
public void setStatus(Integer status) { |
||||||
|
this.status = status; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMsg() { |
||||||
|
return msg; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMsg(String msg) { |
||||||
|
this.msg = msg; |
||||||
|
} |
||||||
|
|
||||||
|
public Object getObj() { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
|
||||||
|
public void setObj(Object obj) { |
||||||
|
this.obj = obj; |
||||||
|
} |
||||||
|
|
||||||
|
private AjaxResponse(Integer status, String msg, Object obj) { |
||||||
|
this.status = status; |
||||||
|
this.msg = msg; |
||||||
|
this.obj = obj; |
||||||
|
} |
||||||
|
|
||||||
|
public static AjaxResponse AjaxData(Integer status, String msg, Object obj) { |
||||||
|
return new AjaxResponse(status, msg, obj); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
package cc.bnblogs.springinit.utils; |
||||||
|
|
||||||
|
public class GlobalConfig { |
||||||
|
/** |
||||||
|
* 测试场景 |
||||||
|
*/ |
||||||
|
public static final Boolean Test = false; |
||||||
|
|
||||||
|
//windows路径
|
||||||
|
public static final String BPMN_PathMapping = "file:D:\\WangJianIDEA_Test\\activiti-imooc\\src\\main\\resources\\resources\\bpmn\\"; |
||||||
|
|
||||||
|
//Liunx路径
|
||||||
|
//public static final String BPMN_PathMapping = "file:/root/Activiti/";
|
||||||
|
|
||||||
|
public enum ResponseCode { |
||||||
|
SUCCESS(0, "成功"), |
||||||
|
ERROR(1, "错误"); |
||||||
|
|
||||||
|
private final int code; |
||||||
|
private final String desc; |
||||||
|
|
||||||
|
ResponseCode(int code, String desc) { |
||||||
|
this.code = code; |
||||||
|
this.desc = desc; |
||||||
|
} |
||||||
|
|
||||||
|
public int getCode() { |
||||||
|
return code; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDesc() { |
||||||
|
return desc; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue