Skip to content

Commit 069a55b

Browse files
committed
自定义授权方式示例添加
1 parent 7025aff commit 069a55b

File tree

4 files changed

+155
-9
lines changed

4 files changed

+155
-9
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
<groupId>org.minbox.framework</groupId>
2222
<artifactId>api-boot-starter-security-oauth-jwt</artifactId>
2323
</dependency>
24+
<!--Hikari-->
25+
<dependency>
26+
<groupId>com.zaxxer</groupId>
27+
<artifactId>HikariCP</artifactId>
28+
</dependency>
29+
<!--MySQL-->
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
</dependency>
2434
</dependencies>
2535
<!--ApiBoot版本依赖-->
2636
<dependencyManagement>
@@ -34,4 +44,13 @@
3444
</dependency>
3545
</dependencies>
3646
</dependencyManagement>
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
<version>2.1.5.RELEASE</version>
53+
</plugin>
54+
</plugins>
55+
</build>
3756
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.minbox.framework.api.boot.plugin.oauth.exception.ApiBootTokenException;
21+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.security.core.GrantedAuthority;
25+
import org.springframework.security.core.userdetails.UserDetails;
26+
import org.springframework.stereotype.Component;
27+
28+
import java.util.Collection;
29+
import java.util.Map;
30+
31+
/**
32+
* 短信验证码登录示例
33+
*
34+
* @author 恒宇少年 - 于起宇
35+
* <p>
36+
* DateTime:2019-06-06 09:15
37+
* Blog:http://blog.yuqiyu.com
38+
* WebSite:http://www.jianshu.com/u/092df3f77bca
39+
* Gitee:https://gitee.com/hengboy
40+
* GitHub:https://github.com/hengboy
41+
*/
42+
@Component
43+
public class PhoneCodeOauthTokenGranter implements ApiBootOauthTokenGranter {
44+
/**
45+
* logger instance
46+
*/
47+
static Logger logger = LoggerFactory.getLogger(PhoneCodeOauthTokenGranter.class);
48+
/**
49+
* 获取Token时使用grant_type=phone_code授权方式
50+
*/
51+
private static final String GRANT_TYPE = "phone_code";
52+
53+
/**
54+
* 参数:手机号
55+
*/
56+
private static final String PARAM_PHONE = "phone";
57+
/**
58+
* 参数:验证码
59+
*/
60+
private static final String PARAM_CODE = "code";
61+
62+
@Override
63+
public String grantType() {
64+
return GRANT_TYPE;
65+
}
66+
67+
/**
68+
* 该方法参数集合是获取Token时携带的参数
69+
* 获取Token路径:/oauth/token?grant_type=phone_code&phone=171xxxxx&code=196523
70+
* phone=171xxxxx
71+
* code=196523
72+
*
73+
* @param parameters parameter map
74+
* @return
75+
* @throws ApiBootTokenException
76+
*/
77+
@Override
78+
public UserDetails loadByParameter(Map<String, String> parameters) throws ApiBootTokenException {
79+
String phone = parameters.get(PARAM_PHONE);
80+
String code = parameters.get(PARAM_CODE);
81+
82+
logger.debug("手机号:{}", phone);
83+
logger.debug("验证码:{}", code);
84+
85+
// 自定义数据逻辑校验验证码是否正确、是否与该手机号匹配等
86+
// 校验通过后返回实现SpringSecurity提供的UserDetails接口的数据实体即可
87+
return new UserDetails() {
88+
@Override
89+
public Collection<? extends GrantedAuthority> getAuthorities() {
90+
return null;
91+
}
92+
93+
@Override
94+
public String getPassword() {
95+
return null;
96+
}
97+
98+
@Override
99+
public String getUsername() {
100+
return phone;
101+
}
102+
103+
@Override
104+
public boolean isAccountNonExpired() {
105+
return true;
106+
}
107+
108+
@Override
109+
public boolean isAccountNonLocked() {
110+
return true;
111+
}
112+
113+
@Override
114+
public boolean isCredentialsNonExpired() {
115+
return true;
116+
}
117+
118+
@Override
119+
public boolean isEnabled() {
120+
return true;
121+
}
122+
};
123+
}
124+
}

api-boot-samples/api-boot-sample-security-oauth-jwt/src/main/resources/application.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
spring:
22
application:
33
name: api-boot-sample-security-oauth-jwt
4+
datasource:
5+
type: com.zaxxer.hikari.HikariDataSource
6+
driver-class-name: com.mysql.cj.jdbc.Driver
7+
url: jdbc:mysql://localhost:3306/test
8+
username: root
9+
password: 123456
410
api:
511
boot:
612
security:
713
# Spring Security 内存方式用户列表示例
8-
#users:
9-
#- username: hengboy
10-
# password: 123456
11-
#- username: apiboot
12-
# password: abc321
13-
enable-default-store-delegate: false
14-
away: jdbc
14+
users:
15+
- username: hengboy
16+
password: 123456
17+
- username: apiboot
18+
password: abc321
1519
oauth:
1620
jwt:
17-
away: jdbc
1821
# 开启Jwt转换AccessToken
1922
enable: true
2023
# 转换Jwt时所需加密key,默认为ApiBoot

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</properties>
2424
<description>
2525
ApiBoot是一款基于SpringBoot1.x、2.x的接口服务集成基础框架,
26-
内部提供了第三方框架的封装集成,让接口开发者可以选着性完成开箱即用
26+
内部提供了第三方框架的封装集成,让接口开发者可以选择性完成开箱即用
2727
不再为搭建接口框架而犯愁,从而极大的提高开发效率。
2828
</description>
2929
<url>https://github.com/hengboy/api-boot</url>

0 commit comments

Comments
 (0)