13 changed files with 351 additions and 21 deletions
@ -0,0 +1,38 @@ |
|||
package com.research.framework.security.provider; |
|||
|
|||
import com.research.framework.security.token.AdminPhoneAuthenticationToken; |
|||
import com.research.framework.web.service.UserDetailsServiceImpl; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.security.authentication.AuthenticationProvider; |
|||
import org.springframework.security.core.Authentication; |
|||
import org.springframework.security.core.AuthenticationException; |
|||
import org.springframework.security.core.userdetails.UserDetails; |
|||
|
|||
import javax.annotation.Resource; |
|||
|
|||
@Slf4j |
|||
public class AdminPhoneAuthenticationProvider implements AuthenticationProvider { |
|||
|
|||
@Resource |
|||
private UserDetailsServiceImpl userDetailsService; |
|||
|
|||
@Override |
|||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
|||
AdminPhoneAuthenticationToken authenticationToken = (AdminPhoneAuthenticationToken) authentication; |
|||
String phone = (String) authentication.getPrincipal(); |
|||
String smsCode = (String) authentication.getCredentials(); |
|||
//通过openId获取用户
|
|||
log.info("通过手机号获取用户:{}", phone); |
|||
UserDetails userDetails = userDetailsService.loadUserByPhone(phone); |
|||
//返回用户信息
|
|||
AdminPhoneAuthenticationToken result = new AdminPhoneAuthenticationToken(userDetails, smsCode, null); |
|||
result.setDetails(authentication.getDetails()); |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public boolean supports(Class<?> authentication) { |
|||
return (AdminPhoneAuthenticationToken.class.isAssignableFrom(authentication)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,81 @@ |
|||
package com.research.framework.security.token; |
|||
|
|||
import org.springframework.security.authentication.AbstractAuthenticationToken; |
|||
import org.springframework.security.core.GrantedAuthority; |
|||
import org.springframework.security.core.SpringSecurityCoreVersion; |
|||
|
|||
import java.util.Collection; |
|||
|
|||
/** |
|||
* @author zhangsan |
|||
* @date 2022-08-11 21:15 |
|||
* @description TODO |
|||
*/ |
|||
public class AdminPhoneAuthenticationToken extends AbstractAuthenticationToken { |
|||
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID; |
|||
|
|||
private final Object principal; |
|||
private String credentials; |
|||
// private final Object detail;
|
|||
|
|||
/** |
|||
* 准备登录时调用 |
|||
* 此构造函数用来初始化未授信凭据. |
|||
* |
|||
* @param principal |
|||
* @param credentials |
|||
*/ |
|||
public AdminPhoneAuthenticationToken(Object principal, String credentials, Object detail) { |
|||
super(null); |
|||
this.principal = principal; |
|||
this.credentials = credentials; |
|||
// this.detail = detail;
|
|||
setAuthenticated(false); |
|||
} |
|||
|
|||
/** |
|||
* 登录成功时调用 |
|||
* 此构函数用来初始化已授信凭据. |
|||
* @param principal |
|||
* @param authorities |
|||
*/ |
|||
public AdminPhoneAuthenticationToken(Object principal, String credentials, Collection<? extends GrantedAuthority> authorities) { |
|||
super(authorities); |
|||
this.principal = principal; |
|||
this.credentials = credentials; |
|||
// this.detail = null;
|
|||
// super.setAuthenticated(true);
|
|||
// super.setAuthenticated(false);
|
|||
super.setAuthenticated(true); |
|||
} |
|||
|
|||
@Override |
|||
public Object getPrincipal() { |
|||
return principal; |
|||
} |
|||
|
|||
@Override |
|||
public Object getCredentials() { |
|||
return credentials; |
|||
} |
|||
|
|||
// @Override
|
|||
// public Object getDetails() {
|
|||
// return detail;
|
|||
// }
|
|||
|
|||
@Override |
|||
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { |
|||
if (isAuthenticated) { |
|||
throw new IllegalArgumentException( |
|||
"Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); |
|||
} |
|||
super.setAuthenticated(false); |
|||
} |
|||
|
|||
@Override |
|||
public void eraseCredentials() { |
|||
super.eraseCredentials(); |
|||
credentials = null; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue