Skip to content

Commit

Permalink
Merge pull request #2 from indyoung/v0.0.9
Browse files Browse the repository at this point in the history
V0.0.9
  • Loading branch information
andanyoung authored Nov 8, 2023
2 parents 8101938 + 7bfc08b commit 121e720
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 75 deletions.
19 changes: 12 additions & 7 deletions admin4j-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.admin4j</groupId>
<artifactId>admin4j-dependencies</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.9.1</version>
<packaging>pom</packaging>

<name>${project.artifactId}</name>
Expand All @@ -17,7 +17,7 @@
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一依赖管理 -->
<spring.boot.version>2.7.14</spring.boot.version>
<spring.boot.version>2.7.17</spring.boot.version>
<spring.cloud.ali.version>2021.0.5.0</spring.cloud.ali.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>

Expand Down Expand Up @@ -64,7 +64,7 @@

<flowable.version>6.7.2</flowable.version>
<admin4j-plugin.version>0.1.2</admin4j-plugin.version>
<admin4j.version>0.9.0-SNAPSHOT</admin4j.version>
<admin4j.version>0.9.0</admin4j.version>
<admin4j-lock.version>0.8.2</admin4j-lock.version>
<admin4j-limiter.version>0.8.0</admin4j-limiter.version>
<admin4j-redis.version>0.8.0</admin4j-redis.version>
Expand Down Expand Up @@ -107,10 +107,15 @@
</dependency>

<!-- admin4j相关-->
<dependency>
<groupId>com.admin4j.dict</groupId>
<artifactId>dict-spring-boot-starter</artifactId>
<version>0.8.1</version>
</dependency>
<dependency>
<groupId>com.admin4j.framework</groupId>
<artifactId>enum-spring-boot-starter</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.admin4j.spring</groupId>
Expand Down Expand Up @@ -290,7 +295,7 @@
<dependency>
<groupId>com.admin4j.framework</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>0.8.8-SNAPSHOT</version>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.admin4j</groupId>
Expand All @@ -301,7 +306,7 @@
<dependency>
<groupId>com.admin4j.framework</groupId>
<artifactId>xxl-job-spring-boot-starter</artifactId>
<version>${admin4j.version}</version>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.github.admin4j</groupId>
Expand Down Expand Up @@ -370,7 +375,7 @@
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
<version>2.0.0</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions admin4j-lock/pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.admin4j</groupId>
<artifactId>framework</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
</parent>

<artifactId>admin4j-lock</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions admin4j-parent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.admin4j</groupId>
<artifactId>admin4j-parent</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.9.0</version>
<description>用于业务框架的父工程</description>
<packaging>pom</packaging>
<name>admin4j-parent</name>
<url>https://github.com/admin4j/admin4j-framework</url>
<properties>
<admin4j-dependencies.version>0.9.0-SNAPSHOT</admin4j-dependencies.version>
<admin4j-dependencies.version>0.9.0</admin4j-dependencies.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion enum-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.admin4j</groupId>
<artifactId>framework</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
</parent>

<groupId>com.admin4j.framework</groupId>
Expand Down
4 changes: 2 additions & 2 deletions oss-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>com.admin4j</groupId>
<artifactId>framework</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
</parent>

<groupId>com.admin4j.framework</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>0.8.8-SNAPSHOT</version>
<version>0.9.0</version>
<packaging>jar</packaging>
<description>封装基于Amazon S3的OSS对象存储服务。在SpringBoot 中通过封装,简单地方式将文件存储到
MinIO、阿里云OSS、华为云OBS、七牛云Kodo、腾讯云COS等支持
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -43,8 +43,8 @@
<module>enum-spring-boot-starter</module>
</modules>
<properties>
<revision>0.9.0-SNAPSHOT</revision>
<admin4j-dependencies.version>0.9.0-SNAPSHOT</admin4j-dependencies.version>
<revision>0.9.0</revision>
<admin4j-dependencies.version>0.9.0</admin4j-dependencies.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
158 changes: 158 additions & 0 deletions security-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# admin security

## USAGES

1. 引入 pom

```
<dependency>
<groupId>com.admin4j.framework</groupId>
<artifactId>security-spring-boot-starter</artifactId>
<version>0.9.0</version>
</dependency>
```

2. 实现 JwtUserDetailsService 接口,用于根据用户ID获取用户详情。由于我们的JWT Token 存的是 userId,所以这里的入参为userId

```java

@Component
public class Admin4jJwtUserDetailsService implements JwtUserDetailsService {

@Autowired
ISysUserLongInfoService sysUserLongInfoService;

@Override
public JwtUserDetails loadUserByUserId(Long userId) {

return sysUserLongInfoService.getByUserId(userId);
}
}
```

3. 账号密码登录。需要实现 `Spring Security``UserDetailsService` 接口,用于根据 username 查询用户详情

```java

@Component
public class Admin4jJwtUserDetailsService implements JwtUserDetailsService, UserDetailsService {

@Autowired
ISysUserLongInfoService sysUserLongInfoService;
@Autowired
ISysUserService sysUserService;

@Override
public JwtUserDetails loadUserByUserId(Long userId) {

return sysUserLongInfoService.getByUserId(userId);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
SysUser sysUser = sysUserService.getByUserName(username);
return SysUserConvert.INSTANCE.convert(sysUser);
}
}

```

### 测试

- 登录接口

```curl
curl --location 'http://localhost:8080/login' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=123456'
```

- 返回结果

```json
{
"code": 200,
"data": {
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzYWx0IjoiMTIiLCJleHAiOjE2OTkzNDU3ODAsInVzZXJJRCI6MX0.tz0RGKSQEwfS0aTrsF7bdxF1enU4Vy32rn4ckDn3-D0"
},
"msg": "success"
}
```

## 多渠道登录

通过配置的方式,支持微信,手机号等多渠道登录

### 验证码手机号登录

- yaml配置方式

```yaml
admin4j:
security:
ignoring:
uris: "login/sendPhoneCode"
multi:
auth-map:
phone: phoneNumber

```

auth-map 为渠道登录登录方式配置。对象类型,key为 登录方式 (`authType`)。value 为登录认证的字段名称。
解释如上配置,

- 登录方式 (`authType`) : `phone`
- 登录认证的字段名称: `phoneNumber`
- ignoring.uris 忽略认证的接口。需要将发送验证码接口,忽略认证。

前端登录接口案例:`/login/phone` (/login/authType)

```curl
curl --location 'http://localhost:8080/login/phone' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'phoneNumber=admin' \
--data-urlencode 'verificationCode=123456'
```

- java 配置代码,实现接口`MultiUserDetailsService`
1. 配置登录方式 `support()` 为 phone (yaml配置的`authType`
2. 检查手机对应的验证码是否正确
3. 根据手机号获取用户详情

```java

/**
* 验证码手机号登录
*
*/
@Component
public class PhoneMultiUserDetailsService implements MultiUserDetailsService {
@Autowired
ISysUserService sysUserService;

@Override
public String support() {
return "phone";
}

@Override
public boolean preVerify(MultiAuthenticationToken multiAuthenticationToken) {

// 验证码
String verificationCode = multiAuthenticationToken.getAuthParameter("verificationCode");
// TODO 验证验证码正确性。错误返回 false,或者抛出错误
return true;
}

@Override
public UserDetails loadUserByMultiToken(String phoneNumber) {

SysUser sysUser = sysUserService.getByPhoneNumber(phoneNumber);
return SysUserConvert.INSTANCE.convert(sysUser);
}
}

```

### 其他渠道登录,如微信openid 登录,参考上方可实现
7 changes: 6 additions & 1 deletion security-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand All @@ -10,11 +10,16 @@
<groupId>com.admin4j.framework</groupId>
<artifactId>security-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<!-- <version>0.9.1-SNAPSHOT</version>-->

<name>security-spring-boot-starter</name>


<dependencies>
<dependency>
<groupId>com.admin4j.common</groupId>
<artifactId>admin4j-common</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.admin4j.framework.security;

import com.admin4j.framework.security.jwt.JwtUserDetails;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -39,5 +38,5 @@ public interface UserTokenService {
* @param token
* @return 登录用户名
*/
UserDetails getUserDetails(String token);
JwtUserDetails getUserDetails(String token);
}
Loading

0 comments on commit 121e720

Please sign in to comment.