# springboot-security-login-simple
**Repository Path**: incloudcode/springboot-security-login-simple
## Basic Information
- **Project Name**: springboot-security-login-simple
- **Description**: https://github.com/yexuejc/yexuejc-springboot 多方登录模块使用例子
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-11-09
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
https://github.com/yexuejc/yexuejc-springboot 多方登录模块使用例子
---------------------------------------------------------------
#### 先上[效果图](Securtity效果图.md)
> **引入依赖 pom.xml**
```mxml
**Security 核心,本文件并未启动Security,需继承;然后继承类上加上@EnableWebSecurity注解就启动Security了。**
* 实现loadUserByUsername()方法;自定义逻辑处理登录账号,返回登录账号相关信息
* 实现loginHodler()方法;自定义处理登录成功filter.setAuthenticationSuccessHandler()和失败filter.setAuthenticationFailureHandler()的处理
* 继承configure(HttpSecurity http) 完善更多security过滤配置
* 例子[com.yexuejc.springboot.base.security.MySecurityConfig](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/security/MySecurityConfig.java)
#### 注: 代码中抛出的相关异常拦截在filter.setAuthenticationFailureHandler()中处理,参考[MySecurityConfig](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/security/MySecurityConfig.java)
```
filter.setAuthenticationFailureHandler((request, response, exception) -> {
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpStatus.UNAUTHORIZED.value());
Resps resps = new Resps();
if (exception instanceof DisabledException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_IS_LOCK_MSG});
} else if (exception instanceof AccountExpiredException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_IS_EXPIRE_MSG});
} else if (exception instanceof CredentialsExpiredException) {
resps.setErr(BizConsts.BASE_LOGIN_IS_EXPIRE_CODE, new String[]{BizConsts.BASE_LOGIN_IS_EXPIRE_MSG});
} else if (exception instanceof LockedException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_IS_LOCKED_MSG});
} else if (exception instanceof AuthenticationCredentialsNotFoundException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_CREDENTIALS_NOT_FOUND_MSG});
} else if (exception instanceof ThirdPartyAuthorizationException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{exception.getMessage()});
} else if (exception instanceof BadCredentialsException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_PWD_IS_ERR_MSG});
} else if (exception instanceof UsernameNotFoundException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_ACCOUNT_NOT_FOUND_MSG});
} else if (exception instanceof UserNotAuthoriayException) {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{exception.getMessage()});
} else {
resps.setErr(RespsConsts.CODE_FAIL, new String[]{BizConsts.BASE_SYS_ERR_MSG});
}
response.getWriter().write(JsonUtil.obj2Json(resps));
response.getWriter().close();
});
```
2.com.yexuejc.springboot.base.security.UserDetailsManager
**获取登录用户信息**
* 需要实现com.yexuejc.springboot.base.security.inte.UserService
* 例子[com.yexuejc.springboot.base.security.UserServiceImpl](../yexuejc-springboot-base/src/test/java/com/yexuejc/springboot/base/security/UserServiceImpl.java)
3.com.yexuejc.springboot.base.security.LoginToken
**登录成功封装至JWT的登录用户信息**
4.com.yexuejc.springboot.base.security.ConsumerUser
**登录成功封装至redis的登录用户信息**
5.com.yexuejc.springboot.base.security.ConsumerToken
**登录请求时(/login)用户登录参数信息**
6.com.yexuejc.springboot.base.security.ConsumerSecurityContextRepository
**登录校验token正确性,返回登录用户(从redis中获取)**
7.com.yexuejc.springboot.base.security.ConsumerAuthenticationProvider
**登录时账号校验(原为密码校验,重写之后增加校验短信验证码,第三方openid)**
8.com.yexuejc.springboot.base.security.ConsumerAuthenticationProcessingFilter
**重写登录拦截,集成多种登录方式到/login**