Skip to content

Commit 7b0b0d8

Browse files
committed
ApiBoot Security Oauth 多模式示例更新
1 parent 556afd8 commit 7b0b0d8

File tree

11 files changed

+275
-86
lines changed

11 files changed

+275
-86
lines changed

api-boot-samples/api-boot-sample-security-oauth-jwt/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<groupId>org.minbox.framework</groupId>
3737
<artifactId>api-boot-starter-mybatis-enhance</artifactId>
3838
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-redis</artifactId>
43+
</dependency>
3944
</dependencies>
4045
<!--ApiBoot版本依赖-->
4146
<dependencyManagement>

api-boot-samples/api-boot-sample-security-oauth-jwt/src/main/java/org/minbox/framework/api/boot/sample/ApiBootSecurityOauthApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import org.minbox.framework.api.boot.common.model.ApiBootResult;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.CommandLineRunner;
68
import org.springframework.boot.SpringApplication;
79
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.data.redis.connection.RedisConnectionFactory;
811
import org.springframework.web.bind.annotation.GetMapping;
912
import org.springframework.web.bind.annotation.RequestMapping;
1013
import org.springframework.web.bind.annotation.RestController;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.sample;
19+
20+
import lombok.AllArgsConstructor;
21+
import lombok.Data;
22+
import lombok.NoArgsConstructor;
23+
import org.springframework.security.core.GrantedAuthority;
24+
import org.springframework.security.core.userdetails.UserDetails;
25+
26+
import java.util.ArrayList;
27+
import java.util.Collection;
28+
29+
/**
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-07-13 09:27
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
@Data
39+
@AllArgsConstructor
40+
@NoArgsConstructor
41+
public class DisableDefaultUserDetails implements UserDetails {
42+
43+
private String username;
44+
private String password;
45+
46+
@Override
47+
public Collection<? extends GrantedAuthority> getAuthorities() {
48+
return new ArrayList() {
49+
{
50+
add((GrantedAuthority) () -> "ROLE_USER");
51+
}
52+
};
53+
}
54+
55+
/**
56+
* 示例密码使用123456
57+
*
58+
* @return
59+
*/
60+
@Override
61+
public String getPassword() {
62+
return password;
63+
}
64+
65+
@Override
66+
public String getUsername() {
67+
return username;
68+
}
69+
70+
@Override
71+
public boolean isAccountNonExpired() {
72+
return true;
73+
}
74+
75+
@Override
76+
public boolean isAccountNonLocked() {
77+
return true;
78+
}
79+
80+
@Override
81+
public boolean isCredentialsNonExpired() {
82+
return true;
83+
}
84+
85+
@Override
86+
public boolean isEnabled() {
87+
return true;
88+
}
89+
}

api-boot-samples/api-boot-sample-security-oauth-jwt/src/main/java/org/minbox/framework/api/boot/sample/DisableDefaultUserTableStoreDelegate.java

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import lombok.Data;
55
import lombok.NoArgsConstructor;
66
import org.minbox.framework.api.boot.plugin.security.delegate.ApiBootStoreDelegate;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.security.core.GrantedAuthority;
89
import org.springframework.security.core.userdetails.UserDetails;
910
import org.springframework.security.core.userdetails.UsernameNotFoundException;
1011
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1112
import org.springframework.stereotype.Component;
13+
import org.springframework.util.ObjectUtils;
1214

1315
import java.util.ArrayList;
1416
import java.util.Collection;
@@ -29,18 +31,8 @@
2931
@Component
3032
public class DisableDefaultUserTableStoreDelegate implements ApiBootStoreDelegate {
3133

32-
/**
33-
* 用户列表示例
34-
* 从该集合内读取用户信息
35-
* 可以使用集合内的用户获取access_token
36-
*/
37-
static List<String> users = new ArrayList() {
38-
{
39-
add("api-boot");
40-
add("hengboy");
41-
add("yuqiyu");
42-
}
43-
};
34+
@Autowired
35+
private SystemUserMapper systemUserMapper;
4436

4537
/**
4638
* 根据用户名查询用户信息
@@ -51,60 +43,10 @@ public class DisableDefaultUserTableStoreDelegate implements ApiBootStoreDelegat
5143
*/
5244
@Override
5345
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
54-
if (!users.contains(username)) {
46+
SystemUser user = systemUserMapper.findByUserName(username);
47+
if (ObjectUtils.isEmpty(user)) {
5548
throw new UsernameNotFoundException("用户:" + username + "不存在");
5649
}
57-
return new DisableDefaultUserDetails(username);
58-
}
59-
60-
@Data
61-
@AllArgsConstructor
62-
@NoArgsConstructor
63-
class DisableDefaultUserDetails implements UserDetails {
64-
private String username;
65-
66-
@Override
67-
public Collection<? extends GrantedAuthority> getAuthorities() {
68-
return new ArrayList() {
69-
{
70-
add((GrantedAuthority) () -> "ROLE_USER");
71-
}
72-
};
73-
}
74-
75-
/**
76-
* 示例密码使用123456
77-
*
78-
* @return
79-
*/
80-
@Override
81-
public String getPassword() {
82-
return new BCryptPasswordEncoder().encode("123456");
83-
}
84-
85-
@Override
86-
public String getUsername() {
87-
return username;
88-
}
89-
90-
@Override
91-
public boolean isAccountNonExpired() {
92-
return true;
93-
}
94-
95-
@Override
96-
public boolean isAccountNonLocked() {
97-
return true;
98-
}
99-
100-
@Override
101-
public boolean isCredentialsNonExpired() {
102-
return true;
103-
}
104-
105-
@Override
106-
public boolean isEnabled() {
107-
return true;
108-
}
50+
return new DisableDefaultUserDetails(user.getUserName(), user.getPassword());
10951
}
11052
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.sample;
19+
20+
import com.gitee.hengboy.mybatis.enhance.common.annotation.Column;
21+
import com.gitee.hengboy.mybatis.enhance.common.annotation.Id;
22+
import com.gitee.hengboy.mybatis.enhance.common.annotation.Table;
23+
import com.gitee.hengboy.mybatis.enhance.common.enums.KeyGeneratorTypeEnum;
24+
import lombok.Data;
25+
26+
import java.io.Serializable;
27+
import java.sql.Timestamp;
28+
29+
/**
30+
* 系统用户信息表
31+
* @author ApiBoot Mybatis Enhance Codegen
32+
*/
33+
@Data
34+
@Table(name = "iot_system_user")
35+
public class SystemUser implements Serializable {
36+
37+
/**
38+
* 主键
39+
*/
40+
@Id(generatorType = KeyGeneratorTypeEnum.UUID)
41+
@Column(name = "SU_ID")
42+
private String id;
43+
/**
44+
* 用户名
45+
*/
46+
@Column(name = "SU_USER_NAME")
47+
private String userName;
48+
/**
49+
* 用户昵称
50+
*/
51+
@Column(name = "SU_NICK_NAME")
52+
private String nickName;
53+
/**
54+
* 用户密码
55+
*/
56+
@Column(name = "SU_PASSWORD")
57+
private String password;
58+
/**
59+
* 用户状态,1:正常,0:冻结,-1:已删除
60+
*/
61+
@Column(name = "SU_STATUS")
62+
private Integer status = 1;
63+
/**
64+
* 创建时间
65+
*/
66+
@Column(name = "SU_CREATE_TIME",insertable = false)
67+
private Timestamp createTime;
68+
/**
69+
* 备注信息
70+
*/
71+
@Column(name = "SU_MARK")
72+
private String mark;
73+
}
74+
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
*
1616
*/
1717

18-
package org.minbox.framework.api.boot.sample.mybatis.enhance;
18+
package org.minbox.framework.api.boot.sample;
1919

2020
import com.gitee.hengboy.mybatis.enhance.mapper.EnhanceMapper;
2121

2222
/**
2323
* @author:恒宇少年 - 于起宇
2424
* <p>
25-
* DateTime:2019-04-26 16:16
25+
* DateTime:2019-07-12 16:14
2626
* Blog:http://blog.yuqiyu.com
2727
* WebSite:http://www.jianshu.com/u/092df3f77bca
2828
* Gitee:https://gitee.com/hengboy
2929
* GitHub:https://github.com/hengboy
3030
*/
31-
public interface UserMapper extends EnhanceMapper<UserEntity, String> {
31+
public interface SystemUserMapper extends EnhanceMapper<SystemUser, String> {
32+
33+
SystemUser findByUserName(String userName);
3234
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.sample;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.stereotype.Service;
22+
23+
/**
24+
* @author:恒宇少年 - 于起宇
25+
* <p>
26+
* DateTime:2019-07-13 09:16
27+
* Blog:http://blog.yuqiyu.com
28+
* WebSite:http://www.jianshu.com/u/092df3f77bca
29+
* Gitee:https://gitee.com/hengboy
30+
* GitHub:https://github.com/hengboy
31+
*/
32+
@Service
33+
public class SystemUserService {
34+
35+
@Autowired
36+
private SystemUserMapper systemUserMapper;
37+
38+
public SystemUser findByUserName(String userName) {
39+
return systemUserMapper.findByUserName(userName);
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
api:
2+
boot:
3+
security:
4+
away: jdbc
5+
enable-default-store-delegate: false
6+
oauth:
7+
away: jdbc
8+
jwt:
9+
enable: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
api:
2+
boot:
3+
security:
4+
away: memory
5+
users:
6+
- username: hengboy
7+
password: 123456
8+
oauth:
9+
away: memory
10+
jwt:
11+
enable: true
12+
# 配置多客户端
13+
clients:
14+
# 客户端test基本配置
15+
- client-id: test
16+
client-secret: 123456
17+
# 客户端admin基本配置
18+
- client-id: admin
19+
client-secret: 123456

0 commit comments

Comments
 (0)