martin 3 жил өмнө
parent
commit
8f901e105f

+ 6 - 4
api-common/src/main/java/api/common/pojo/po/UserPO.java

@@ -1,9 +1,7 @@
 package api.common.pojo.po;
 
-import api.common.pojo.common.CommonPO;
 import lombok.AllArgsConstructor;
 import lombok.Data;
-import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
@@ -11,16 +9,20 @@ import java.io.Serializable;
 /**
  * 用户。
  */
-@EqualsAndHashCode(callSuper = true)
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
-public class UserPO extends CommonPO implements Serializable {
+public class UserPO implements Serializable {
 
     private String id;              // 用户主键(唯一)
     private String username;        // 登录用户名
     private String nickname;        // 用户昵称,用于显示
     private String password;        // 密码(加密)
+    private String phone;
+    private String job;
+    private String isSub;
+    private String parentId;
+    private String role;
     private String isDeleted = "0";
 
 }

+ 53 - 43
api-common/src/main/java/api/common/util/CollectionUtil.java

@@ -1,41 +1,24 @@
 package api.common.util;
 
 import java.util.*;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
 
 public class CollectionUtil {
 
-    // -------------------------------- A --------------------------------
+    //* -------------------------------- before jdk9 --------------------------------
+
     public static <T> List<T> arrayToList(T[] array) {
         return Arrays.asList(array);
     }
 
-    // -------------------------------- B --------------------------------
-    // -------------------------------- C --------------------------------
-    @SafeVarargs
+
     public static <T> ArrayList<T> createArrayList(T... elements) {
         return new ArrayList<>(Arrays.asList(elements));
     }
 
-    // -------------------------------- D --------------------------------
-    // -------------------------------- F --------------------------------
-
-    /**
-     * 根据过滤条件来过滤列表
-     *
-     * @param arrayList 原始列表
-     * @param predicate 过滤条件的 lambda 表达式,a-> a.getColor().equals("red") && a.getWeight() > 300
-     * @param <T>       声明为泛型方法
-     * @return 结果列表
-     */
-    public static <T> List<T> filterList(List<T> arrayList, Predicate<? super T> predicate) {
-        return arrayList.stream().filter(predicate).collect(Collectors.toList());
+    public static <T> HashSet<T> createHashSet(T... elements) {
+        return new HashSet<>(Arrays.asList(elements));
     }
 
-    // -------------------------------- G --------------------------------
-    // -------------------------------- H --------------------------------
-    // -------------------------------- I --------------------------------
     public static boolean isEmpty(Collection<?> collection) {
         return collection == null || collection.isEmpty();
     }
@@ -48,31 +31,58 @@ public class CollectionUtil {
         return !isEmpty(collection);
     }
 
-    public static boolean isNotOne(Collection<?> collection) {
-        return isEmpty(collection) || collection.size() > 1;
-    }
-
     public static boolean isNotEmpty(Map<?, ?> map) {
         return !isEmpty(map);
     }
 
-    // -------------------------------- J --------------------------------
-    // -------------------------------- K --------------------------------
-    // -------------------------------- L --------------------------------
-    // -------------------------------- M --------------------------------
-    // -------------------------------- N --------------------------------
-    // -------------------------------- O --------------------------------
-    // -------------------------------- P --------------------------------
-    // -------------------------------- Q --------------------------------
-    // -------------------------------- R --------------------------------
-    // -------------------------------- S --------------------------------
-    // -------------------------------- T --------------------------------
-    // -------------------------------- U --------------------------------
-    // -------------------------------- V --------------------------------
-    // -------------------------------- W --------------------------------
-    // -------------------------------- X --------------------------------
-    // -------------------------------- Y --------------------------------
-    // -------------------------------- Z --------------------------------
+//    //* -------------------------------- jdk9  --------------------------------
+//
+//    /**
+//     * 根据数组或参数序列创建只读的 list
+//     *
+//     * @param elements 数组或参数序列
+//     * @param <T>      元素类型
+//     * @return 只读 list
+//     */
+//    @SafeVarargs
+//    public static <T> List<T> createUnmodifiableList(T... elements) {
+//        return List.of(elements);
+//    }
+//
+//    /**
+//     * 根据 list 创建只读的 list
+//     *
+//     * @param list list
+//     * @param <E>  元素类型
+//     * @return 只读 list
+//     */
+//    public static <E> List<E> createUnmodifiableList(List<E> list) {
+//        return Collections.unmodifiableList(list);
+//    }
+//
+//    /**
+//     * 根据 set 创建只读的 set
+//     *
+//     * @param set set
+//     * @param <E> 元素类型
+//     * @return 只读 list
+//     */
+//    public static <E> Set<E> createUnmodifiableList(Set<E> set) {
+//        return Collections.unmodifiableSet(set);
+//    }
+//
+//
+//    /**
+//     * 根据 map 创建只读的 map
+//     *
+//     * @param map map
+//     * @param <K> 键类型
+//     * @param <V> 值类型
+//     * @return 只读 map
+//     */
+//    public static <K, V> Map<K, V> createUnmodifiableList(Map<K, V> map) {
+//        return Collections.unmodifiableMap(map);
+//    }
 
 
 }

+ 27 - 0
simulation-gateway/pom.xml

@@ -36,6 +36,33 @@
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
         </dependency>
         <!-- nacos - 结束 -->
+
+        <!-- 基础 -->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-loadbalancer</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 8 - 0
simulation-oauth-server/pom.xml

@@ -73,6 +73,14 @@
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
         </dependency>
         <!-- nacos - 结束 -->
+
+
+        <!-- api-common -->
+        <dependency>
+            <groupId>com.css</groupId>
+            <artifactId>api-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <!-- 基础 -->
         <dependency>
             <groupId>org.projectlombok</groupId>

+ 31 - 21
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/cofiguration/oauth/MyAccessTokenConverter.java

@@ -1,24 +1,34 @@
-//package com.css.simulation.oauth.server.cofiguration.oauth;
-//
-//import org.springframework.context.annotation.Bean;
-//import org.springframework.context.annotation.Configuration;
-//import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
-//import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
-//import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
-//
-//import javax.annotation.Resource;
-//
-//@Configuration
-//public class MyAccessTokenConverter {
-//
-//    @Resource
-//    private UserAuthenticationConverter userAuthenticationConverter;
-//
+package com.css.simulation.oauth.server.cofiguration.oauth;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+
+@Configuration
+public class MyAccessTokenConverter {
+
+    @Autowired
+    MyUserAuthenticationConverter myUserAuthenticationConverter;
+
+    /**
+     * redis 存储需使用
+     */
+    @Bean
+    public AccessTokenConverter accessTokenConverter() {
+        DefaultAccessTokenConverter defaultAccessTokenConverter = new DefaultAccessTokenConverter();
+        defaultAccessTokenConverter.setUserTokenConverter(myUserAuthenticationConverter);
+        return defaultAccessTokenConverter;
+    }
+
+//    /**
+//     * jwt 令牌存储需使用
+//     */
 //    @Bean
 //    public AccessTokenConverter accessTokenConverter() {
-//        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
-//        accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
-//        return accessTokenConverter;
+//        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
+//        jwtAccessTokenConverter.setSigningKey("project");
+//        return jwtAccessTokenConverter;
 //    }
-//
-//}
+}

+ 13 - 9
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/cofiguration/oauth/MyTokenService.java

@@ -1,5 +1,8 @@
 package com.css.simulation.oauth.server.cofiguration.oauth;
 
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.security.oauth2.provider.ClientDetailsService;
@@ -7,19 +10,19 @@ import org.springframework.security.oauth2.provider.token.AuthorizationServerTok
 import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
 import org.springframework.security.oauth2.provider.token.TokenStore;
 
-import javax.annotation.Resource;
-
+@Data
 @Configuration
+@ConfigurationProperties("oauth.token")
 public class MyTokenService {
 
-    @Resource
-    private ClientDetailsService clientDetailsService;  // 客户端详情服务
-
+    private int accessTokenValiditySeconds;
+    private int refreshTokenValiditySeconds;
 
-    @Resource
+    @Autowired
+    private ClientDetailsService clientDetailsService;  // 客户端详情服务
+    @Autowired
     private TokenStore tokenStore;  // 客户端详情服务
 
-
     /**
      * 令牌管理策略
      */
@@ -29,8 +32,9 @@ public class MyTokenService {
         services.setClientDetailsService(clientDetailsService);     // 客户端详情服务,获取 ClientDetailsServiceConfigurer 中配置的客户端
         services.setSupportRefreshToken(true);      // 允许令牌自动刷新
         services.setTokenStore(tokenStore);         // 令牌存储策略
-        services.setAccessTokenValiditySeconds(7200);   // 令牌默认有效期 2 小时
-        services.setRefreshTokenValiditySeconds(259200);    // 刷新令牌默认有效期 3 天。
+//        services.setTokenEnhancer(jwtAccessTokenConverter);         // 使用 jwt 令牌
+        services.setAccessTokenValiditySeconds(accessTokenValiditySeconds);   // 令牌默认有效期 2 小时
+        services.setRefreshTokenValiditySeconds(refreshTokenValiditySeconds);    // 刷新令牌默认有效期 3 天。
         return services;
     }
 

+ 28 - 20
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/cofiguration/oauth/MyUserAuthenticationConverter.java

@@ -1,20 +1,28 @@
-//package com.css.simulation.oauth.server.cofiguration.oauth;
-//
-//import org.springframework.security.core.Authentication;
-//import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
-//import org.springframework.stereotype.Component;
-//
-//import java.util.LinkedHashMap;
-//import java.util.Map;
-//
-//@Component
-//public class MyUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
-//
-//    @Override
-//    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
-//        Map<String, Object> response = new LinkedHashMap<>();
-//        response.put(USERNAME, authentication.getName());
-//        return response;
-//    }
-//
-//}
+package com.css.simulation.oauth.server.cofiguration.oauth;
+
+import com.css.simulation.oauth.server.cofiguration.security.MyUserDetails;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.authority.AuthorityUtils;
+import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+@Component
+public class MyUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
+
+    @Override
+    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
+        Map<String, Object> response = new LinkedHashMap<>();
+        MyUserDetails myUserDetails = (MyUserDetails) authentication.getPrincipal();
+        //1 用户基本信息
+        response.put("username", authentication.getName());
+        response.put("phone", myUserDetails.getPhone());
+        //2 用户权限信息
+        if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
+            response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
+        }
+        return response;
+    }
+}

+ 10 - 7
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/cofiguration/security/MyUserDetails.java

@@ -1,25 +1,28 @@
 package com.css.simulation.oauth.server.cofiguration.security;
 
 import lombok.AllArgsConstructor;
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.userdetails.UserDetails;
 
-import java.util.Collection;
+import java.io.Serializable;
+import java.util.Set;
 
 @Data
+@Builder
 @NoArgsConstructor
 @AllArgsConstructor
-public class MyUserDetails implements UserDetails {
-
+public class MyUserDetails implements UserDetails, Serializable {
+    private static final long serialVersionUID = -158357727659030597L;
     private String username;
     private String password;
+    private String phone;
+    private String isSub;
+    private String parentId;
+    private Set<GrantedAuthority> authorities;
 
-    @Override
-    public Collection<? extends GrantedAuthority> getAuthorities() {
-        return null;
-    }
 
     /**
      * 默认 false 是将用户账号过期,需改成 true 不过期

+ 9 - 2
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/cofiguration/security/MyUserDetailsService.java

@@ -1,8 +1,10 @@
 package com.css.simulation.oauth.server.cofiguration.security;
 
 
+import api.common.pojo.po.UserPO;
+import api.common.util.CollectionUtil;
 import com.css.simulation.oauth.server.mapper.UserMapper;
-import com.css.simulation.oauth.server.pojo.po.UserPO;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -19,9 +21,14 @@ public class MyUserDetailsService implements UserDetailsService {
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         UserPO userPO = userMapper.selectByUsername(username);
+
         return new MyUserDetails(
                 userPO.getUsername(),
-                userPO.getPassword()
+                userPO.getPassword(),
+                userPO.getPhone(),
+                userPO.getIsSub(),
+                userPO.getParentId(),
+                CollectionUtil.createHashSet(new SimpleGrantedAuthority("default11111111111"))
         );
     }
 

+ 9 - 3
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/mapper/UserMapper.java

@@ -1,7 +1,7 @@
 package com.css.simulation.oauth.server.mapper;
 
 
-import com.css.simulation.oauth.server.pojo.po.UserPO;
+import api.common.pojo.po.UserPO;
 import org.apache.ibatis.annotations.*;
 import org.apache.ibatis.type.JdbcType;
 
@@ -16,11 +16,17 @@ public interface UserMapper {
     @Results(id = "user", value = {
             @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
             @Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
-            @Result(property = "password", column = "password", jdbcType = JdbcType.VARCHAR)
+            @Result(property = "password", column = "password", jdbcType = JdbcType.VARCHAR),
+            @Result(property = "phone", column = "phone", jdbcType = JdbcType.VARCHAR),
+            @Result(property = "is_sub", column = "is_sub", jdbcType = JdbcType.VARCHAR),
+            @Result(property = "parent_id", column = "parent_id", jdbcType = JdbcType.VARCHAR)
     })
     @Select("select id,\n" +
             "       username,\n" +
-            "       password\n" +
+            "       password,\n" +
+            "       phone,\n" +
+            "       is_sub,\n" +
+            "       parent_id\n" +
             "from system_user\n" +
             "where is_deleted = '0'" +
             "   and username = #{username}")

+ 0 - 25
simulation-oauth-server/src/main/java/com/css/simulation/oauth/server/pojo/po/UserPO.java

@@ -1,25 +0,0 @@
-package com.css.simulation.oauth.server.pojo.po;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-import java.io.Serializable;
-
-/**
- * 用户。
- */
-@Data
-@NoArgsConstructor
-@AllArgsConstructor
-public class UserPO implements Serializable {
-
-    private String id;              // 用户主键(唯一)
-    private String username;        // 登录用户名
-    private String nickname;        // 用户昵称,用于显示
-    private String password;        // 密码(加密)
-    private String phone;
-    private String job;
-    private String isDeleted = "0";
-
-}