Browse Source

代码修改

LAPTOP-KB9HII50\70708 1 year ago
parent
commit
e7f336f3a7

+ 24 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/exception/PwdException.java

@ -0,0 +1,24 @@
package com.yihu.jw.security.exception;
import org.springframework.security.oauth2.common.exceptions.ClientAuthenticationException;
/**
 * Created by wsl on 2022/9/20
 */
public class PwdException extends ClientAuthenticationException {
    public PwdException(String msg, Throwable t) {
        super(msg, t);
    }
    public PwdException(String msg) {
        super(msg);
    }
    @Override
    public String getOAuth2ErrorCode() {
         return "pwd_low  error";
    }
}

+ 76 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/oauth2/provider/WlyyTokenGranter.java

@ -127,6 +127,15 @@ public class WlyyTokenGranter implements TokenGranter {
                        requestFactory,
                        userDetailsService
                ));
        tokenGranters.put(PwdAndCaptchaTokenGranter.GRANT_TYPE,
                new PwdAndCaptchaTokenGranter(
                        authenticationManager,
                        tokenServices,
                        clientDetailsService,
                        requestFactory,
                        userDetailsService,
                        wlyyRedisVerifyCodeService
                ));
    }
    public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
@ -650,5 +659,72 @@ public class WlyyTokenGranter implements TokenGranter {
            return new OAuth2Authentication(storedOAuth2Request, userAuth);
        }
    }
    /**
     * 密码+验证码登录
     */
    public static class PwdAndCaptchaTokenGranter extends AbstractTokenGranter {
        private static final String GRANT_TYPE = "pwdAndCaptcha";
        private final AuthenticationManager authenticationManager;
        private final UserDetailsService userDetailsService;
        private final WlyyRedisVerifyCodeService wlyyRedisVerifyCodeService;
        private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
        public PwdAndCaptchaTokenGranter(AuthenticationManager authenticationManager,
                                         AuthorizationServerTokenServices tokenServices,
                                         ClientDetailsService clientDetailsService,
                                         OAuth2RequestFactory requestFactory,
                                         UserDetailsService userDetailsService,
                                         WlyyRedisVerifyCodeService wlyyRedisVerifyCodeService) {
            this(authenticationManager, tokenServices, clientDetailsService, requestFactory, userDetailsService,wlyyRedisVerifyCodeService, GRANT_TYPE);
        }
        protected PwdAndCaptchaTokenGranter(AuthenticationManager authenticationManager,
                                            AuthorizationServerTokenServices tokenServices,
                                            ClientDetailsService clientDetailsService,
                                            OAuth2RequestFactory requestFactory,
                                            UserDetailsService userDetailsService,
                                            WlyyRedisVerifyCodeService wlyyRedisVerifyCodeService,
                                            String grantType) {
            super(tokenServices, clientDetailsService, requestFactory, grantType);
            this.authenticationManager = authenticationManager;
            this.wlyyRedisVerifyCodeService = wlyyRedisVerifyCodeService;
            this.userDetailsService = userDetailsService;
        }
        @Override
        protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
            Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
            String client_id = parameters.get("client_id");
            String username = parameters.get("username");
            String captcha = parameters.get("captcha");
            //todo cyx  部署应取消注释(自测试,可注释,不验证短信直接登录)
            if (!wlyyRedisVerifyCodeService.verification(client_id, username, captcha)){
                throw new InvalidGrantException("Invalid captcha");
            }
            String password = parameters.get("password");
            parameters.remove("password");
            Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password);
            ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
            try {
                userAuth = authenticationManager.authenticate(userAuth);
            }
            catch (AccountStatusException ase) {
                throw new InvalidGrantException(ase.getMessage());
            }
            catch (BadCredentialsException e) {
                throw new InvalidGrantException(e.getMessage());
            }
            if (userAuth == null || !userAuth.isAuthenticated()) {
                throw new InvalidGrantException("Could not authenticate user: " + username);
            }
            OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
            return new OAuth2Authentication(storedOAuth2Request, userAuth);
        }
    }
}

+ 19 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/oauth2/provider/endpoint/WlyyLoginEndpoint.java

@ -410,6 +410,14 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
                Oauth2Envelop<WlyyUserSimple> oauth2Envelop = new Oauth2Envelop<>("获取用户手机号失败", -1, null);
                return new ResponseEntity<>(oauth2Envelop, headers, HttpStatus.OK);
            }
        } else if("1".equals(parameters.get("pwdAndCaptcha"))){
            parameters.put("grant_type", "pwdAndCaptcha");
            KeyPair keyPair = (KeyPair) httpSession.getAttribute("privateKey");
            String password = com.yihu.jw.security.utils.RSAUtils.decryptBase64(parameters.get("password"), keyPair);
            parameters.put("password", password);
//            if (!testPwd(parameters.get("password"))) {
//                throw new PwdException("密码强度低,请修改密码后再重新登录!");
//            }
        } else if (StringUtils.isEmpty(parameters.get("captcha"))) {
            parameters.put("grant_type", "password");
            //解密密码
@ -524,6 +532,17 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
        return getResponse(wlyyUserSimple);
    }
    private boolean testPwd(String pwd){
        //3种
        String PW_PATTERN2 = "^(?![A-Za-z0-9]+$)(?![A-Za-z\\W]+$)[a-zA-Z0-9_\\W]{8,}$";
        if(org.apache.commons.lang3.StringUtils.isBlank(pwd)){
            return false;
        }
        if(pwd.length()<8||pwd.length()>20){
            return false;
        }
        return pwd.matches(PW_PATTERN2);
    }
    /**
     * 单点登陆第二步 - token验证