登录密码验证代码

244 阅读1分钟

@Override
protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
    //String username = authentication.getPrincipal ().toString ();
    //校内用户
    String username = userDetails.getUsername();
    if (!(authentication instanceof SLAuthenticationToken) && !((BaseUser) userDetails).isOutside ()) {
            //                String presentedPassword = authentication.getCredentials().toString();
//            HashMap<String,Object> map=new HashMap();
//                map.put("username", username);
//                map.put("password", presentedPassword);
//                JSONObject result;
//                try {
//                    restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
//                        @Override
//                        public void handleError(ClientHttpResponse response) throws IOException{
//                            if (response.getRawStatusCode()!=401){
//                                super.handleError(response);
//                            }
//                        }
//                    });
//                    result = restTemplate.getForObject(securityConfiguration.getSchoolUrl().getSchoolUserCheckUrl(),
//                            JSONObject.class,map);
//                    System.out.println(result);
//                } catch (Exception e) {
//                    log.error("check user {} fail for {}", username, e.getMessage());
//                    throw new SLSecurityException("认证服务器连接异常");
//                }
//                //TODO 需要根据实际返回进行修改
//                if (!Langs.isBlank(result.getString("access_token"))) {
//                    log.info("user {} login success!!", username);
//                } else {
//                    throw new BadCredentialsException("用户名或密码错误");
//                }
    } else if (!(authentication instanceof SLAuthenticationToken) && ((BaseUser) userDetails).isOutside ()) {
        String presentedPassword = authentication.getCredentials ().toString ();
        if (!passwordEncoder.matches (presentedPassword, userDetails.getPassword ())) {
            throw new BadCredentialsException ("密码错误");
        }
    } else if (authentication instanceof SLAuthenticationToken) {
        SLToken slToken = ((SLAuthenticationToken) authentication).getSlToken ();
        if (!(slToken.getUserId ().equals(((BaseUser) userDetails).getId ()) &&
                slToken.getUsername ().equals (username))) {
            throw new BadCredentialsException ("token信息有误");
        }
    } else {
        throw new BadCredentialsException ("无此支持方式");
    }
}

注释部分为密码验证代码