forked from fp2952/spring-cloud-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fangp
committed
Jun 16, 2018
1 parent
6758068
commit 5e4e4a9
Showing
15 changed files
with
796 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../src/main/java/com/peng/auth/provider/config/auth/filter/MyLoginAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.peng.auth.provider.config.auth.filter; | ||
|
||
import com.peng.auth.provider.config.auth.token.MyAuthenticationToken; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.authentication.AuthenticationServiceException; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by fp295 on 2018/6/16. | ||
* 自定义登陆filter,新增登陆方式:验证码、二维码扫码、账号密码; | ||
* 此filter 为生成自定义的 MyAuthenticationToken | ||
*/ | ||
public class MyLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter { | ||
|
||
public static final String SPRING_SECURITY_RESTFUL_TYPE_PHONE = "phone"; | ||
public static final String SPRING_SECURITY_RESTFUL_TYPE_QR = "qr"; | ||
public static final String SPRING_SECURITY_RESTFUL_TYPE_DEFAULT = "user"; | ||
|
||
// 登陆类型:user:用户密码登陆;phone:手机验证码登陆;qr:二维码扫码登陆 | ||
private static final String SPRING_SECURITY_RESTFUL_TYPE_KEY = "type"; | ||
// 登陆终端:1:移动端登陆,包括微信公众号、小程序等;0:PC后台登陆 | ||
private static final String SPRING_SECURITY_RESTFUL_MOBILE_KEY = "mobile"; | ||
private static final String SPRING_SECURITY_RESTFUL_USERNAME_KEY = "username"; | ||
private static final String SPRING_SECURITY_RESTFUL_PASSWORD_KEY = "password"; | ||
private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "phone"; | ||
private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "verifyCode"; | ||
private static final String SPRING_SECURITY_RESTFUL_QR_CODE_KEY = "qrCode"; | ||
|
||
private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/login"; | ||
private boolean postOnly = true; | ||
|
||
public MyLoginAuthenticationFilter() { | ||
super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST")); | ||
} | ||
|
||
|
||
@Override | ||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { | ||
if (postOnly && !request.getMethod().equals("POST")) { | ||
throw new AuthenticationServiceException( | ||
"Authentication method not supported: " + request.getMethod()); | ||
} | ||
|
||
String type = obtainParameter(request, SPRING_SECURITY_RESTFUL_TYPE_KEY); | ||
String mobile = obtainParameter(request, SPRING_SECURITY_RESTFUL_MOBILE_KEY); | ||
MyAuthenticationToken authRequest; | ||
String principal; | ||
String credentials; | ||
|
||
// 手机验证码登陆 | ||
if(SPRING_SECURITY_RESTFUL_TYPE_PHONE.equals(type)){ | ||
principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY); | ||
credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY); | ||
} | ||
// 二维码扫码登陆 | ||
else if(SPRING_SECURITY_RESTFUL_TYPE_QR.equals(type)){ | ||
principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_QR_CODE_KEY); | ||
credentials = null; | ||
} | ||
// 账号密码登陆 | ||
else { | ||
principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_USERNAME_KEY); | ||
credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_PASSWORD_KEY); | ||
|
||
} | ||
if (principal == null) { | ||
principal = ""; | ||
} | ||
if (credentials == null) { | ||
credentials = ""; | ||
} | ||
principal = principal.trim(); | ||
authRequest = new MyAuthenticationToken( | ||
principal, credentials, type, mobile); | ||
// Allow subclasses to set the "details" property | ||
setDetails(request, authRequest); | ||
return this.getAuthenticationManager().authenticate(authRequest); | ||
} | ||
|
||
private void setDetails(HttpServletRequest request, | ||
AbstractAuthenticationToken authRequest) { | ||
authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); | ||
} | ||
|
||
private String obtainParameter(HttpServletRequest request, String parameter) { | ||
return request.getParameter(parameter); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...r/src/main/java/com/peng/auth/provider/config/auth/handler/MyLoginAuthSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.peng.auth.provider.config.auth.handler; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; | ||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; | ||
import org.springframework.security.web.savedrequest.RequestCache; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* Created by fp295 on 2018/6/16. | ||
* 登陆成功处理,移动端登陆成功后还需做绑定操作 | ||
*/ | ||
public class MyLoginAuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { | ||
private RequestCache requestCache = new HttpSessionRequestCache(); | ||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
System.out.println("onAuthenticationSuccess"); | ||
super.onAuthenticationSuccess(request, response, authentication); | ||
} | ||
} |
Oops, something went wrong.