ludc
2023-09-14 1c66d394049a07320f108c282b792b80e512778c
代码提交
已修改9个文件
已删除1个文件
已添加1个文件
251 ■■■■■ 文件已修改
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/config/BladeResourceServerConfiguration.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/endpoint/BladeTokenEndPoint.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/endpoint/PwdFreeLoginEndpoint.java 95 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/BladeTokenGranter.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/PwdFreeLoginTokenGranter.java 116 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/SocialTokenGranter.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-gateway/src/main/java/com/vci/ubcs/gateway/provider/AuthProvider.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/controller/PasswordFreeLoginController.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/IPasswordFreeLoginService.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/config/BladeResourceServerConfiguration.java
@@ -38,7 +38,6 @@
            .antMatchers(
                "/actuator/**",
                "/oauth/captcha",
                "/oauth/passwordFreeLogin",
                "/oauth/logout",
                "/oauth/clear-cache",
                "/oauth/render/**",
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/endpoint/BladeTokenEndPoint.java
@@ -32,7 +32,9 @@
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
@@ -59,6 +61,7 @@
import java.security.Principal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/endpoint/PwdFreeLoginEndpoint.java
ÎļþÒÑɾ³ý
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/BladeTokenGranter.java
@@ -45,6 +45,8 @@
        granters.add(new CaptchaTokenGranter(authenticationManager, endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), bladeRedis));
        // å¢žåŠ ç¬¬ä¸‰æ–¹ç™»é™†æ¨¡å¼
        granters.add(new SocialTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), userClient, socialProperties));
        // å¢žåŠ å…å¯†ç™»é™†æ¨¡å¼
        //granters.add(new PwdFreeLoginTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), userClient, socialProperties));
        // ç»„合tokenGranter集合
        return new CompositeTokenGranter(granters);
    }
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/PwdFreeLoginTokenGranter.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,116 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill åº„骞 (smallchill@163.com)
 */
package com.vci.ubcs.auth.granter;
import com.vci.ubcs.auth.constant.AuthConstant;
import com.vci.ubcs.auth.service.BladeUserDetails;
import com.vci.ubcs.auth.utils.TokenUtil;
import com.vci.ubcs.system.user.entity.User;
import com.vci.ubcs.system.user.entity.UserInfo;
import com.vci.ubcs.system.user.entity.UserOauth;
import com.vci.ubcs.system.user.feign.IUserClient;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import org.springblade.core.social.props.SocialProperties;
import org.springblade.core.social.utils.SocialUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
 * ç¬¬ä¸‰æ–¹ç™»å½•认证类
 *
 * @author Chill
 */
public class PwdFreeLoginTokenGranter extends AbstractTokenGranter {
    private static final String GRANT_TYPE = "passwordfree";
    private static final Integer AUTH_SUCCESS_CODE = 2000;
    private final AuthenticationManager authenticationManager;
    public PwdFreeLoginTokenGranter(AuthenticationManager authenticationManager, AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.authenticationManager = authenticationManager;
    }
    @Override
    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
        // è¯·æ±‚头租户信息
        HttpServletRequest request = WebUtil.getRequest();
        String tenantId = Func.toStr(request.getHeader(TokenUtil.TENANT_HEADER_KEY), TokenUtil.DEFAULT_TENANT_ID);
        // èŽ·å–è¯·æ±‚å‚æ•°
        Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());
        // æ ¹æ®å‚数进行自定义的授权逻辑
        // ç¤ºä¾‹ä¸­ä½¿ç”¨äº†ç¡¬ç¼–码的方式验证账号和生成授权信息
        String userName = parameters.get("username");
        String password = parameters.get("password");
        // ç»„装数据
        //UserOauth userOauth = Objects.requireNonNull(BeanUtil.copy(authUser, UserOauth.class));
        //userOauth.setTenantId(tenantId);
        //userOauth.setUuid(authUser.getUuid());
        // è¿œç¨‹è°ƒç”¨ï¼ŒèŽ·å–è®¤è¯ä¿¡æ¯
        //R<UserInfo> result = userClient.userAuthInfo(userOauth);
        BladeUserDetails bladeUserDetails = null;
        // é…ç½®çš„密码,所有走免密接口的都设置统一的密码
        if ("password".equals(password)) {
            // æž„建授权信息
            //User user = result.getData().getUser();
            //Kv detail = result.getData().getDetail();
//            if (user == null || user.getId() == null) {
//                throw new InvalidGrantException("social grant failure, user is null");
//            }
//            bladeUserDetails = new BladeUserDetails(user.getId(),
//                tenantId, result.getData().getOauthId(), userName, "webservice免密登录", "0", "0", "0", "0", Func.toStr(userOauth.getAvatar(), TokenUtil.DEFAULT_AVATAR),
//                userName, AuthConstant.ENCRYPT + password, detail, true, true, true, true,
//                AuthorityUtils.commaSeparatedStringToAuthorityList(Func.join(result.getData().getRoles())));
        } else {
            throw new InvalidGrantException("passwordfree grant failure, auth response is not success");
        }
        // ç»„装认证数据,关闭密码校验
        Authentication userAuth = new UsernamePasswordAuthenticationToken(bladeUserDetails, null, bladeUserDetails.getAuthorities());
        ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
        OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
        // è¿”回 OAuth2Authentication
        return new OAuth2Authentication(storedOAuth2Request, userAuth);
    }
}
Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/granter/SocialTokenGranter.java
@@ -42,6 +42,7 @@
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
Source/UBCS/ubcs-gateway/src/main/java/com/vci/ubcs/gateway/provider/AuthProvider.java
@@ -34,6 +34,7 @@
    static {
        DEFAULT_SKIP_URL.add("/example");
        DEFAULT_SKIP_URL.add("/oauth/token/**");
        DEFAULT_SKIP_URL.add("/oauth/password-free-login/**");
        DEFAULT_SKIP_URL.add("/oauth/captcha/**");
        DEFAULT_SKIP_URL.add("/oauth/clear-cache/**");
        DEFAULT_SKIP_URL.add("/oauth/user-info");
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/controller/PasswordFreeLoginController.java
@@ -3,9 +3,7 @@
import com.vci.ubcs.code.service.IPasswordFreeLoginService;
import io.swagger.annotations.Api;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -21,9 +19,9 @@
    @Resource
    private IPasswordFreeLoginService passwordFreeLoginService;
    @GetMapping("/login")
    public R passwordFreeLogin(String account) {
        boolean resBoolean = passwordFreeLoginService.passwordFreeLogin(account);
    @PostMapping("/login")
    public R passwordFreeLogin(@RequestParam String username) {
        boolean resBoolean = passwordFreeLoginService.passwordFreeLogin(username);
        return R.status(resBoolean);
    }
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/IPasswordFreeLoginService.java
@@ -9,10 +9,10 @@
    /**
     * å…å¯†ç™»å½•方法
     * @param account è´¦å·
     * @param username è´¦å·
     * @return
     */
    boolean passwordFreeLogin(String account);
    boolean passwordFreeLogin(String username);
}
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
@@ -3724,6 +3724,7 @@
        if (listR.getData().isEmpty()) {
            throw new VciBaseException("传入业务类型未查询到相应表单,请检查!");
        }
        // TODO:参照配置的模糊查询过滤条件暂未处理
        String namesql = "";
        if (StringUtils.isNotBlank(baseQueryObject.getConditionMap().get("name"))) {
            String s = baseQueryObject.getConditionMap().get("name");
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java
@@ -47,24 +47,29 @@
    /**
     * å…å¯†ç™»å½•
     * @param account è´¦å·
     * @param username è´¦å·
     * @return
     */
    @Override
    public boolean passwordFreeLogin(String account) {
    public boolean passwordFreeLogin(String username) {
        // å…å¯†ç™»å½•接口地址
        String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/passwordFreeLogin?username=admin&grant_type=captcha&scope=all&type=account";
        String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/password-free-login";
        // è®¾ç½®è¯·æ±‚头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("Authorization", "Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
        headers.set("Tenant-Id", "000000");
        //设置请求体参数
        MultiValueMap<String,String> bodyParams = new LinkedMultiValueMap<String,String>();
        bodyParams.add("account",account);
        MultiValueMap<String,String> parameters = new LinkedMultiValueMap<String,String>();
        parameters.add("username",username);
        parameters.add("grant_type", "captcha");
        parameters.add("scope", "all");
        parameters.add("type", "account");
        // å‘送POST请求
        String responseBody = HttpUtils.post(loginUrl, bodyParams);
        String responseBody = HttpUtils.post(loginUrl, parameters,headers);
        System.out.println(responseBody);
        //拿到响应体将token存入到redis中,以account作为存储的key