├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── UPDATE.md
├── pom.xml
└── src
├── doc
└── table.sql
├── main
├── java
│ └── club
│ │ └── yuit
│ │ ├── CodeGenerator.java
│ │ └── oauth
│ │ └── boot
│ │ ├── BootApplication.java
│ │ ├── authentication
│ │ └── sms
│ │ │ ├── SmsAuthenticationProvider.java
│ │ │ ├── SmsCodeAuthenticationFilter.java
│ │ │ ├── SmsCodeAuthenticationToken.java
│ │ │ ├── SmsCodeCheckFilter.java
│ │ │ └── SmsSecurityConfig.java
│ │ ├── config
│ │ ├── CoreConfig.java
│ │ ├── DbConfig.java
│ │ ├── RedisConfig.java
│ │ ├── SecurityConfig.java
│ │ └── auth2
│ │ │ ├── OAuth2AuthorizationServerConfig.java
│ │ │ ├── OAuth2ResourceServerConfig.java
│ │ │ └── TokenStoreConfig.java
│ │ ├── controller
│ │ ├── BaseMainController.java
│ │ ├── BootGrantController.java
│ │ ├── ClientController.java
│ │ ├── CodeController.java
│ │ └── TestController.java
│ │ ├── entity
│ │ ├── Client.java
│ │ └── User.java
│ │ ├── exception
│ │ ├── ArgumentsFailureException.java
│ │ ├── AuthFailureException.java
│ │ ├── NotAuthException.java
│ │ ├── NotAuthorityException.java
│ │ └── VerificationCodeFailureException.java
│ │ ├── filter
│ │ ├── BootBasicAuthenticationFilter.java
│ │ └── BootPictureCodeAuthenticationFilter.java
│ │ ├── handler
│ │ ├── BootLoginFailureHandler.java
│ │ └── ExceptionAdviceHandler.java
│ │ ├── mapper
│ │ ├── ClientMapper.java
│ │ └── UserMapper.java
│ │ ├── response
│ │ ├── BaseResponse.java
│ │ ├── HttpResponse.java
│ │ ├── HttpStatusAndMsg.java
│ │ ├── Items.java
│ │ ├── ListResponse.java
│ │ ├── OrderType.java
│ │ ├── PageAndSortResponse.java
│ │ ├── PageQueryItems.java
│ │ └── SimpleResponse.java
│ │ ├── service
│ │ ├── IClientService.java
│ │ ├── IUserService.java
│ │ └── impl
│ │ │ ├── ClientServiceImpl.java
│ │ │ └── UserServiceImpl.java
│ │ ├── support
│ │ ├── BootSecurityProperties.java
│ │ ├── BootSmsUserDetailService.java
│ │ ├── BootUserDetailService.java
│ │ ├── DefaultBeanName.java
│ │ ├── code
│ │ │ ├── BootCodeService.java
│ │ │ ├── RedisCodeService.java
│ │ │ ├── SessionCodeService.java
│ │ │ ├── VerificationCode.java
│ │ │ ├── VerificationCodeGenerator.java
│ │ │ ├── picture
│ │ │ │ └── DefaultPictureCodeGenerator.java
│ │ │ └── sms
│ │ │ │ └── DefaultSmsCodeGenerator.java
│ │ ├── common
│ │ │ └── TokenStoreType.java
│ │ ├── oauth2
│ │ │ ├── BootAccessDeniedHandler.java
│ │ │ ├── BootClientDetails.java
│ │ │ ├── BootClientDetailsService.java
│ │ │ ├── BootOAuth2AuthExceptionEntryPoint.java
│ │ │ ├── BootOAuth2Exception.java
│ │ │ ├── BootOAuth2WebResponseExceptionTranslator.java
│ │ │ └── BootOAuthExceptionJacksonSerializer.java
│ │ └── properities
│ │ │ ├── BootBaseLoginProperties.java
│ │ │ ├── BootLogLevelProperties.java
│ │ │ ├── BootOAuth2Properties.java
│ │ │ ├── BootSmsLoginProperties.java
│ │ │ └── CodeStoreType.java
│ │ └── utils
│ │ ├── CommonUtils.java
│ │ └── HttpUtils.java
└── resources
│ ├── application.yml
│ ├── mapper
│ └── UserMapper.xml
│ ├── statics
│ ├── favicon.png
│ ├── libs
│ │ ├── layer
│ │ │ ├── layer.js
│ │ │ └── theme
│ │ │ │ └── default
│ │ │ │ ├── icon-ext.png
│ │ │ │ ├── icon.png
│ │ │ │ ├── layer.css
│ │ │ │ ├── loading-0.gif
│ │ │ │ ├── loading-1.gif
│ │ │ │ └── loading-2.gif
│ │ └── now-ui
│ │ │ ├── css
│ │ │ ├── bootstrap.min.css
│ │ │ ├── now-ui-kit.css
│ │ │ └── now-ui-kit.min.css
│ │ │ ├── fonts
│ │ │ ├── nucleo-license.md
│ │ │ ├── nucleo-outline.eot
│ │ │ ├── nucleo-outline.ttf
│ │ │ ├── nucleo-outline.woff
│ │ │ └── nucleo-outline.woff2
│ │ │ ├── img
│ │ │ ├── apple-icon.png
│ │ │ ├── avatar.jpg
│ │ │ ├── bg1.jpg
│ │ │ ├── bg11.jpg
│ │ │ ├── bg3.jpg
│ │ │ ├── bg4.jpg
│ │ │ ├── bg5.jpg
│ │ │ ├── bg6.jpg
│ │ │ ├── bg7.jpg
│ │ │ ├── bg8.jpg
│ │ │ ├── blurred-image-1.jpg
│ │ │ ├── creative-tim-white-slim2.png
│ │ │ ├── default-avatar.png
│ │ │ ├── eva.jpg
│ │ │ ├── favicon.png
│ │ │ ├── flags
│ │ │ │ ├── AD.png
│ │ │ │ ├── AE.png
│ │ │ │ ├── AG.png
│ │ │ │ ├── AM.png
│ │ │ │ ├── AR.png
│ │ │ │ ├── AT.png
│ │ │ │ ├── AU.png
│ │ │ │ ├── BE.png
│ │ │ │ ├── BF.png
│ │ │ │ ├── BG.png
│ │ │ │ ├── BO.png
│ │ │ │ ├── BR.png
│ │ │ │ ├── CA.png
│ │ │ │ ├── CD.png
│ │ │ │ ├── CG.png
│ │ │ │ ├── CH.png
│ │ │ │ ├── CL.png
│ │ │ │ ├── CM.png
│ │ │ │ ├── CN.png
│ │ │ │ ├── CO.png
│ │ │ │ ├── CZ.png
│ │ │ │ ├── DE.png
│ │ │ │ ├── DJ.png
│ │ │ │ ├── DK.png
│ │ │ │ ├── DZ.png
│ │ │ │ ├── EE.png
│ │ │ │ ├── EG.png
│ │ │ │ ├── ES.png
│ │ │ │ ├── FI.png
│ │ │ │ ├── FR.png
│ │ │ │ ├── GA.png
│ │ │ │ ├── GB.png
│ │ │ │ ├── GM.png
│ │ │ │ ├── GT.png
│ │ │ │ ├── HN.png
│ │ │ │ ├── HT.png
│ │ │ │ ├── HU.png
│ │ │ │ ├── ID.png
│ │ │ │ ├── IE.png
│ │ │ │ ├── IL.png
│ │ │ │ ├── IN.png
│ │ │ │ ├── IQ.png
│ │ │ │ ├── IR.png
│ │ │ │ ├── IT.png
│ │ │ │ ├── JM.png
│ │ │ │ ├── JO.png
│ │ │ │ ├── JP.png
│ │ │ │ ├── KG.png
│ │ │ │ ├── KN.png
│ │ │ │ ├── KP.png
│ │ │ │ ├── KR.png
│ │ │ │ ├── KW.png
│ │ │ │ ├── KZ.png
│ │ │ │ ├── LA.png
│ │ │ │ ├── LB.png
│ │ │ │ ├── LC.png
│ │ │ │ ├── LS.png
│ │ │ │ ├── LU.png
│ │ │ │ ├── LV.png
│ │ │ │ ├── MG.png
│ │ │ │ ├── MK.png
│ │ │ │ ├── ML.png
│ │ │ │ ├── MM.png
│ │ │ │ ├── MT.png
│ │ │ │ ├── MX.png
│ │ │ │ ├── NA.png
│ │ │ │ ├── NE.png
│ │ │ │ ├── NG.png
│ │ │ │ ├── NI.png
│ │ │ │ ├── NL.png
│ │ │ │ ├── NO.png
│ │ │ │ ├── OM.png
│ │ │ │ ├── PA.png
│ │ │ │ ├── PE.png
│ │ │ │ ├── PG.png
│ │ │ │ ├── PK.png
│ │ │ │ ├── PL.png
│ │ │ │ ├── PT.png
│ │ │ │ ├── PY.png
│ │ │ │ ├── QA.png
│ │ │ │ ├── RO.png
│ │ │ │ ├── RU.png
│ │ │ │ ├── RW.png
│ │ │ │ ├── SA.png
│ │ │ │ ├── SE.png
│ │ │ │ ├── SG.png
│ │ │ │ ├── SL.png
│ │ │ │ ├── SN.png
│ │ │ │ ├── SO.png
│ │ │ │ ├── SV.png
│ │ │ │ ├── TD.png
│ │ │ │ ├── TJ.png
│ │ │ │ ├── TL.png
│ │ │ │ ├── TR.png
│ │ │ │ ├── TZ.png
│ │ │ │ ├── UA.png
│ │ │ │ ├── US.png
│ │ │ │ ├── VE.png
│ │ │ │ ├── VN.png
│ │ │ │ └── YE.png
│ │ │ ├── header.jpg
│ │ │ ├── hero-image-1.png
│ │ │ ├── hero-image-2.png
│ │ │ ├── hero-image-3.png
│ │ │ ├── invision-white-slim.png
│ │ │ ├── julie.jpg
│ │ │ ├── landing.jpg
│ │ │ ├── login.jpg
│ │ │ ├── logo-square.jpg
│ │ │ ├── logo.png
│ │ │ ├── now-logo.png
│ │ │ ├── nucleo-logo.svg
│ │ │ ├── profile.jpg
│ │ │ └── ryan.jpg
│ │ │ └── js
│ │ │ ├── core
│ │ │ ├── bootstrap.min.js
│ │ │ ├── jquery.min.js
│ │ │ └── popper.min.js
│ │ │ ├── now-ui-kit.js
│ │ │ ├── now-ui-kit.js.map
│ │ │ ├── now-ui-kit.min.js
│ │ │ └── plugins
│ │ │ ├── bootstrap-datepicker.js
│ │ │ ├── bootstrap-switch.js
│ │ │ └── nouislider.min.js
│ └── styles
│ │ ├── base-grant.css
│ │ ├── base-login.css
│ │ └── base.css
│ └── views
│ ├── base-grant.html
│ ├── base-login.html
│ └── commons
│ └── head.html
└── test
└── java
├── Main.java
└── club
└── yuit
└── oauth
└── boot
└── BootApplicationTests.java
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.js linguist-language=java
2 | *.css linguist-language=java
3 | *.html linguist-language=java
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 yuit
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #### OAUTH-BOOT
2 |
3 | spring-security ,spring-security-oauth2 ,string boot 学习
4 |
5 | ---
6 |
7 | #### Update
8 | [更新说明](https://github.com/LookBackInTheRain/oauth-boot/blob/dev/UPDATE.md)
9 | #### Current
10 | 1. 授权码模式,密码模式,简化模式(未测试),客户端模式(未测试)
11 | 2. JWT
12 | 3. 自定义登录页面和授权页面
13 | 4. 自定义异常处理
14 | 5. [认证服务与资源服务分离](https://github.com/LookBackInTheRain/oauth-boot-up)
15 |
16 | #### 配置
17 |
18 | ```yaml
19 | boot:
20 | oauth:
21 | # token 存储方式,可选配置
22 | token-store-type: jwt #默认为 memory
23 | # token签名秘钥,可选配置,默认:OAUTHBOOT@IUY09&098#UIOKNJJ-YUIT.CLUB
24 | token-signing-key: 123qwe
25 | # 登录处理url 可选配置
26 | login-process-url: /auth/authorize
27 | ```
28 |
29 | #### 授权码模式
30 |
31 | 1. 请求授权 http://ip:port/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:9000&scope=select
32 | 2. 如果没有登录会跳转到登录页面,登录后跳转到授权页面(是否会跳转到授权页面取决于是否将isAutoApprove字段的值 )
33 | 3. 授权后得到一个授权码,拿着授权码即可申请token
34 |
35 | #### 密码模式
36 | 没有配置允许客户端表单登录的,将客户端id和密码base64编码放入请求头中,根据oauth2协议规定的密码模式正确填写参数即可申请token
37 |
38 | #### 依赖
39 |
40 | |框架/类库/数据库| 版本号 |
41 | |--|--|
42 | |java|11(Mac)/ 8(Win10)|
43 | | spring-boot | 2.0.5.RELEASE |
44 | |spring-security|5.0.8.RELEASE |
45 | |spring-security-oauth2-autoconfigure|2.0.6.RELEASE|
46 | |mybatis-plus|3.0.4|
47 | |数据库连接池(druid)|1.1.11|
48 | |swagger-ui|2.9.2|
49 | |hibernate-validator|6.0.13.Final|
50 | |MySQL|5.7.22 MySQL Community Server|
51 | |Redis|4.0.10|
52 |
53 | #### 项目效果
54 | 1. 自定义登录和授权页面效果图
55 | 
56 |
57 | #### 建表语句在src/doc/table.sql中
58 |
59 | 相关的测试数据也在这个sql文件中,加密的密码统一为123qwe
60 |
61 | #### 注
62 |
63 | 请使用上述依赖所规定的版本
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/UPDATE.md:
--------------------------------------------------------------------------------
1 |
2 | ### 2019/4/9 更新
3 | 1. [认证服务与资源服务分离](https://github.com/LookBackInTheRain/oauth-boot-up)
4 | ### 2019/3/8 更新
5 | 1. 解决无法使用 `refresh_token` 更新`token`问题
6 | 2. `spring-boot` 升级并测试通过
7 |
8 | |框架/类库/数据库| 旧版本 | 新版本|
9 | |--|--|--|
10 | |java|11(Mac)/ 8(Win10)| - |
11 | | spring-boot | 2.0.5.RELEASE | 2.1.3.RELEASE |
12 | |spring-security|5.0.8.RELEASE | 5.0.8.RELEASE |
13 | |spring-security-oauth2-autoconfigure|2.0.6.RELEASE| 2.1.3.RELEASE |
14 | |mybatis-plus|3.0.4| 3.1.0 |
15 | |数据库连接池(druid)|1.1.11| 1.1.14 |
16 | |swagger-ui|2.9.2| - |
17 | |hibernate-validator|6.0.13.Final| - |
18 | |MySQL|5.7.22 MySQL Community Server| - |
19 | |Redis|4.0.10| - |
20 |
21 | ### 2019/2/15 更新
22 | 1. 配置token存储类型增加jdbc,已测试通过
23 | ```
24 | 使用jdbc时必须创建的表的SQL语句在/src/doc/table.sql中,建表语句参考 :
25 | https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql
26 | ```
27 | 2. 修复 [#4](https://github.com/LookBackInTheRain/oauth-boot/issues/4)
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | club.yuit
7 | oauth-boot
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | oauth-boot
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.4.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | javax.xml.bind
34 | jaxb-api
35 | 2.3.1
36 |
37 |
38 |
39 |
40 | javax.activation
41 | activation
42 | 1.1
43 |
44 |
45 |
46 | org.glassfish.jaxb
47 | jaxb-runtime
48 | 2.3.1
49 |
50 |
51 |
52 |
53 |
54 | org.springframework.boot
55 | spring-boot-starter-web
56 |
57 |
58 |
59 | org.springframework.boot
60 | spring-boot-starter-security
61 |
62 |
63 |
64 | org.springframework.security
65 | spring-security-jwt
66 | 1.0.9.RELEASE
67 |
68 |
69 |
70 |
75 |
76 |
77 | org.springframework.security.oauth.boot
78 | spring-security-oauth2-autoconfigure
79 | 2.1.3.RELEASE
80 |
81 |
82 |
83 |
84 | org.springframework.boot
85 | spring-boot-starter-data-redis
86 |
87 |
88 |
89 | org.springframework.boot
90 | spring-boot-devtools
91 | runtime
92 |
93 |
94 | mysql
95 | mysql-connector-java
96 | runtime
97 |
98 |
99 | org.projectlombok
100 | lombok
101 | true
102 |
103 |
104 |
105 | org.springframework.boot
106 | spring-boot-starter-test
107 | test
108 |
109 |
110 |
111 | org.springframework.boot
112 | spring-boot-configuration-processor
113 | true
114 |
115 |
116 |
117 |
118 | com.baomidou
119 | mybatis-plus-boot-starter
120 | 3.1.0
121 |
122 |
123 |
124 | com.baomidou
125 | mybatis-plus-generator
126 | 3.1.0
127 |
128 |
129 |
130 |
131 | com.alibaba
132 | druid
133 | 1.1.14
134 |
135 |
136 |
137 |
138 | io.springfox
139 | springfox-swagger2
140 | 2.9.2
141 |
142 |
143 |
144 | io.springfox
145 | springfox-swagger-ui
146 | 2.9.2
147 |
148 |
149 |
150 |
151 |
152 |
153 | org.hibernate
154 | hibernate-validator
155 | 6.0.13.Final
156 |
157 |
158 |
159 | org.apache.commons
160 | commons-lang3
161 |
162 |
163 |
164 | org.springframework.boot
165 | spring-boot-starter-thymeleaf
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 | org.springframework.boot
177 | spring-boot-maven-plugin
178 |
179 |
180 |
181 |
182 |
183 |
184 |
--------------------------------------------------------------------------------
/src/doc/table.sql:
--------------------------------------------------------------------------------
1 | use boot;
2 |
3 | -- 创建用户表
4 | create table user
5 | (
6 | id varchar(32) primary key,
7 | username varchar(255),
8 | password varchar(255),
9 | gender varchar(10) ,
10 | mobile varchar(16),
11 | email varchar(100),
12 | isEnable bit,
13 | isExpired bit,
14 | isLocked bit,
15 | createTime timestamp default current_timestamp comment '创建时间',
16 | modifyTime timestamp default current_timestamp on update current_timestamp comment '更新时间'
17 | );
18 |
19 | -- 客户端表
20 | create table clients (
21 | id varchar(32) primary key,
22 | clientId varchar(100) not null,
23 | resourceIds varchar(255),
24 | isSecretRequired bit,
25 | clientSecret varchar(100),
26 | isScoped bit,
27 | scope varchar(255),
28 | authorizedGrantTypes varchar(255) not null,
29 | registeredRedirectUri varchar(255) not null,
30 | authorities varchar(255),
31 | isAutoApprove bit,
32 | accessTokenValiditySeconds int ,
33 | refreshTokenValiditySeconds int,
34 | createTime timestamp default current_timestamp comment '创建时间',
35 | modifyTime timestamp default current_timestamp on update current_timestamp comment '更新时间'
36 | );
37 |
38 | -- user 测试数据 密码123qwe
39 | INSERT INTO boot.user (id, username,mobile, password, gender, email, isEnable, isExpired, isLocked) VALUES ('67842834823', 'admin','18785471131', '$2a$10$06S5v7Mo47e8Qyv65Ltz.uhcQwfhIcgYDKVPVzBlPj6UHWV2ErbzK', '女', '阿斯达@as.com', true, false, true);
40 |
41 | -- clients 测试数据 密码123qwe
42 | INSERT INTO boot.clients (id, clientId, resourceIds, isSecretRequired, clientSecret, isScoped, scope, authorizedGrantTypes, registeredRedirectUri, authorities, isAutoApprove, accessTokenValiditySeconds, refreshTokenValiditySeconds, createTime, modifyTime) VALUES ('JKGJHGJHFGH89867', 'client', 'boot-server', true, '$2a$10$06S5v7Mo47e8Qyv65Ltz.uhcQwfhIcgYDKVPVzBlPj6UHWV2ErbzK', true, 'select', 'refresh_token,authorization_code,password', 'http://localhost:9000', 'CLIENT,ADMIN', false, 1800, 36000, '2018-10-16 10:02:14', '2018-12-14 09:05:03');
43 |
44 |
45 | -- 当token-store-type: jdbc时 需要创建如下两张表
46 | create table oauth_access_token
47 | (
48 | token_id VARCHAR(255),
49 | token longblob,
50 | authentication_id VARCHAR(255) PRIMARY KEY,
51 | user_name VARCHAR(255),
52 | client_id VARCHAR(255),
53 | authentication longblob,
54 | refresh_token VARCHAR(255)
55 | );
56 |
57 | create table oauth_refresh_token
58 | (
59 | token_id VARCHAR(256),
60 | token longblob,
61 | authentication longblob
62 | );
63 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/CodeGenerator.java:
--------------------------------------------------------------------------------
1 | package club.yuit;
2 |
3 | import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
4 | import com.baomidou.mybatisplus.core.toolkit.StringPool;
5 | import com.baomidou.mybatisplus.core.toolkit.StringUtils;
6 | import com.baomidou.mybatisplus.generator.AutoGenerator;
7 | import com.baomidou.mybatisplus.generator.InjectionConfig;
8 | import com.baomidou.mybatisplus.generator.config.*;
9 | import com.baomidou.mybatisplus.generator.config.po.TableInfo;
10 | import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
11 | import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 | import java.util.Scanner;
16 |
17 | /**
18 | * @author yuit
19 | * @date 2018/10/9 17:24
20 | * 代码生成器
21 | **/
22 | public class CodeGenerator {
23 |
24 | /**
25 | *
focList = new ArrayList<>();
79 | focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
80 | @Override
81 | public String outputFile(TableInfo tableInfo) {
82 | // 自定义输入文件名称
83 | return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
84 | + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
85 | }
86 | });
87 | cfg.setFileOutConfigList(focList);
88 | mpg.setCfg(cfg);
89 | mpg.setTemplate(new TemplateConfig().setXml(null));
90 |
91 | // 策略配置
92 | StrategyConfig strategy = new StrategyConfig();
93 | strategy.setNaming(NamingStrategy.underline_to_camel);
94 | strategy.setColumnNaming(NamingStrategy.underline_to_camel);
95 | strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
96 | strategy.setEntityLombokModel(true);
97 | strategy.setRestControllerStyle(true);
98 | strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
99 | strategy.setInclude(scanner("表名"));
100 | strategy.setSuperEntityColumns("id");
101 | strategy.setControllerMappingHyphenStyle(true);
102 | strategy.setTablePrefix(pc.getModuleName() + "_");
103 | mpg.setStrategy(strategy);
104 | mpg.setTemplateEngine(new FreemarkerTemplateEngine());
105 | mpg.execute();
106 | }
107 |
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/BootApplication.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.ConfigurableApplicationContext;
7 |
8 | /**
9 | * @author yuit
10 | */
11 | @SpringBootApplication
12 | @MapperScan("club.yuit.oauth.boot.mapper")
13 | public class BootApplication {
14 |
15 |
16 |
17 | public static void main(String[] args) {
18 | ConfigurableApplicationContext context= SpringApplication.run(BootApplication.class, args);
19 |
20 | }
21 |
22 |
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/authentication/sms/SmsAuthenticationProvider.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.authentication.sms;
2 |
3 | import lombok.Getter;
4 | import lombok.Setter;
5 | import org.springframework.data.redis.core.StringRedisTemplate;
6 | import org.springframework.security.authentication.AuthenticationProvider;
7 | import org.springframework.security.authentication.InternalAuthenticationServiceException;
8 | import org.springframework.security.core.Authentication;
9 | import org.springframework.security.core.AuthenticationException;
10 | import org.springframework.security.core.userdetails.UserDetails;
11 | import org.springframework.security.core.userdetails.UserDetailsService;
12 |
13 | /**
14 | * @author yuit
15 | * @date 2018/10/19 15:33
16 | */
17 | @Getter
18 | @Setter
19 | public class SmsAuthenticationProvider implements AuthenticationProvider {
20 |
21 | private UserDetailsService userDetailsService;
22 |
23 |
24 | public SmsAuthenticationProvider() {
25 | }
26 |
27 | @Override
28 | public Authentication authenticate(Authentication authentication) throws AuthenticationException {
29 |
30 | SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication;
31 |
32 | UserDetails user = this.userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());
33 |
34 | if (user == null) {
35 | throw new InternalAuthenticationServiceException("无法获取用户信息");
36 | }
37 |
38 | SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(user,user.getAuthorities());
39 |
40 | authenticationResult.setDetails(authenticationToken.getDetails());
41 |
42 | return authenticationResult;
43 | }
44 |
45 | @Override
46 | public boolean supports(Class> authentication) {
47 | return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/authentication/sms/SmsCodeAuthenticationFilter.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.authentication.sms;
2 |
3 | import club.yuit.oauth.boot.support.BootSecurityProperties;
4 | import org.springframework.security.authentication.AuthenticationServiceException;
5 | import org.springframework.security.core.Authentication;
6 | import org.springframework.security.core.AuthenticationException;
7 | import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
8 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
9 | import org.springframework.util.Assert;
10 |
11 | import javax.servlet.http.HttpServletRequest;
12 | import javax.servlet.http.HttpServletResponse;
13 |
14 | /**
15 | * @author yuit
16 | * @date 2018/10/19 15:33
17 | */
18 | public class SmsCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
19 |
20 |
21 | public static final String BOOT_FORM_MOBILE_KEY = "mobile";
22 |
23 | private String mobileParameter = BOOT_FORM_MOBILE_KEY;
24 | private boolean postOnly = true;
25 |
26 |
27 | public SmsCodeAuthenticationFilter(String path) {
28 | super(new AntPathRequestMatcher(path, "POST"));
29 | }
30 |
31 | // ~ Methods
32 | // ========================================================================================================
33 | @Override
34 | public Authentication attemptAuthentication(HttpServletRequest request,
35 | HttpServletResponse response) throws AuthenticationException {
36 | if (postOnly && !request.getMethod().equalsIgnoreCase("POST")) {
37 | throw new AuthenticationServiceException(
38 | "Authentication method not supported: " + request.getMethod());
39 | }
40 |
41 | String mobile = obtainMobile(request);
42 |
43 |
44 | if (mobile == null) {
45 | mobile = "";
46 | }
47 |
48 |
49 |
50 | mobile = mobile.trim();
51 |
52 | SmsCodeAuthenticationToken authRequest = new SmsCodeAuthenticationToken(mobile);
53 |
54 | // Allow subclasses to set the "details" property
55 | setDetails(request,authRequest);
56 |
57 | return this.getAuthenticationManager().authenticate(authRequest);
58 | }
59 |
60 |
61 | /**
62 | * 获取手机号
63 | * @param request
64 | * @return
65 | */
66 | protected String obtainMobile(HttpServletRequest request) {
67 | return request.getParameter(mobileParameter);
68 | }
69 |
70 | /**
71 | * Provided so that subclasses may configure what is put into the authentication
72 | * request's details property.
73 | *
74 | * @param request that an authentication request is being created for
75 | * @param authRequest the authentication request object that should have its details
76 | * set
77 | */
78 | protected void setDetails(HttpServletRequest request,
79 | SmsCodeAuthenticationToken authRequest) {
80 | authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
81 | }
82 |
83 | /**
84 | * Sets the parameter name which will be used to obtain the username from the login
85 | * request.
86 | *
87 | * @param usernameParameter the parameter name. Defaults to "username".
88 | */
89 | public void setMobileParameter(String usernameParameter) {
90 | Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
91 | this.mobileParameter = usernameParameter;
92 | }
93 |
94 |
95 |
96 | /**
97 | * Defines whether only HTTP POST requests will be allowed by this filter. If set to
98 | * true, and an authentication request is received which is not a POST request, an
99 | * exception will be raised immediately and authentication will not be attempted. The
100 | * unsuccessfulAuthentication() method will be called as if handling a failed
101 | * authentication.
102 | *
103 | * Defaults to true but may be overridden by subclasses.
104 | */
105 | public void setPostOnly(boolean postOnly) {
106 | this.postOnly = postOnly;
107 | }
108 |
109 | public final String getMobileParameter() {
110 | return mobileParameter;
111 | }
112 |
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/authentication/sms/SmsCodeAuthenticationToken.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.authentication.sms;
2 |
3 | import org.springframework.security.authentication.AbstractAuthenticationToken;
4 | import org.springframework.security.core.GrantedAuthority;
5 | import org.springframework.security.core.SpringSecurityCoreVersion;
6 |
7 | import java.util.Collection;
8 |
9 | /**
10 | * @author yuit
11 | * @date 2018/10/19 15:33
12 | */
13 | public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
14 |
15 | private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
16 |
17 | // ~ Instance fields
18 | // ================================================================================================
19 |
20 | private final Object principal;
21 | // 这里存放登录相关的信息,用户名,密码等,但是在认证之前已经把短信验证码校验过,这里不需要
22 | // private Object credentials;
23 |
24 | // ~ Constructors
25 | // ===================================================================================================
26 |
27 | /**
28 | * This constructor can be safely used by any code that wishes to create a
29 | * UsernamePasswordAuthenticationToken
, as the {@link #isAuthenticated()}
30 | * will return false
.
31 | *
32 | */
33 | public SmsCodeAuthenticationToken(String mobile) {
34 | super(null);
35 | this.principal = mobile;
36 | setAuthenticated(false);
37 | }
38 |
39 | /**
40 | * This constructor should only be used by AuthenticationManager
or
41 | * AuthenticationProvider
implementations that are satisfied with
42 | * producing a trusted (i.e. {@link #isAuthenticated()} = true
)
43 | * authentication token.
44 | *
45 | * @param principal
46 | * @param authorities
47 | */
48 | public SmsCodeAuthenticationToken(Object principal,
49 | Collection extends GrantedAuthority> authorities) {
50 | super(authorities);
51 | this.principal = principal;
52 | super.setAuthenticated(true); // must use super, as we override
53 | }
54 |
55 | // ~ Methods
56 | // ========================================================================================================
57 |
58 | public Object getCredentials() {
59 | return null;
60 | }
61 |
62 | public Object getPrincipal() {
63 | return this.principal;
64 | }
65 |
66 | public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
67 | if (isAuthenticated) {
68 | throw new IllegalArgumentException(
69 | "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
70 | }
71 |
72 | super.setAuthenticated(false);
73 | }
74 |
75 | @Override
76 | public void eraseCredentials() {
77 | super.eraseCredentials();
78 | }
79 | }
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/authentication/sms/SmsCodeCheckFilter.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.authentication.sms;
2 |
3 | import club.yuit.oauth.boot.exception.VerificationCodeFailureException;
4 | import club.yuit.oauth.boot.support.BootSecurityProperties;
5 | import lombok.Getter;
6 | import lombok.Setter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.apache.commons.lang3.StringUtils;
9 | import org.slf4j.Logger;
10 | import org.slf4j.LoggerFactory;
11 | import org.springframework.data.redis.core.StringRedisTemplate;
12 | import org.springframework.security.core.AuthenticationException;
13 | import org.springframework.security.web.authentication.AuthenticationFailureHandler;
14 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
15 | import org.springframework.util.AntPathMatcher;
16 | import org.springframework.util.PathMatcher;
17 | import org.springframework.web.filter.OncePerRequestFilter;
18 |
19 | import javax.servlet.FilterChain;
20 | import javax.servlet.ServletException;
21 | import javax.servlet.http.HttpServletRequest;
22 | import javax.servlet.http.HttpServletResponse;
23 | import java.io.IOException;
24 |
25 | /**
26 | * @author yuit
27 | * @date 2018/10/19 15:33
28 | */
29 | @Getter
30 | @Setter
31 | @Slf4j
32 | public class SmsCodeCheckFilter extends OncePerRequestFilter {
33 |
34 |
35 | private AuthenticationFailureHandler failureHandler;
36 | private BootSecurityProperties properties;
37 | private StringRedisTemplate template;
38 | private AuthenticationSuccessHandler successHandler;
39 | private PathMatcher pathMatcher;
40 |
41 |
42 |
43 | public SmsCodeCheckFilter(BootSecurityProperties properties) {
44 | setProperties(properties);
45 | this.pathMatcher = new AntPathMatcher();
46 |
47 | }
48 |
49 | @Override
50 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
51 |
52 | if(this.pathMatcher.match("/authentication/mobile",request.getRequestURI())
53 | && StringUtils.equalsAnyIgnoreCase(request.getMethod(),"post")){
54 | try {
55 | check(request,response,filterChain);
56 | }catch (Exception ex){
57 | if (ex instanceof VerificationCodeFailureException){
58 | failureHandler.onAuthenticationFailure(request,response, (AuthenticationException) ex);
59 | }
60 | throw ex;
61 | }
62 |
63 | }else {
64 | filterChain.doFilter(request,response);
65 | }
66 | }
67 |
68 | private void check(HttpServletRequest request, HttpServletResponse response,FilterChain chain) throws VerificationCodeFailureException, IOException, ServletException {
69 |
70 | String mobile = request.getParameter(properties.getSmsLogin().getMobileParameterName());
71 | String code = request.getParameter(properties.getSmsLogin().getCodeParameterName());
72 | if (mobile.trim().length()==0) {
73 | throw new VerificationCodeFailureException("手机号不能为空");
74 | }
75 |
76 | if (!this.template.hasKey(mobile)) {
77 | throw new VerificationCodeFailureException("验证码过期或手机号错误");
78 | }
79 |
80 | Long expireTime= this.template.getExpire(mobile);
81 |
82 | if (expireTime<=0){
83 | throw new VerificationCodeFailureException("验证码过期");
84 | }
85 |
86 | String redisCode = this.template.opsForValue().get(mobile);
87 |
88 | if (!code.equals(redisCode)) {
89 | throw new VerificationCodeFailureException("验证码错误");
90 | }
91 |
92 | chain.doFilter(request,response);
93 | }
94 |
95 |
96 |
97 | }
98 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/authentication/sms/SmsSecurityConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.authentication.sms;
2 |
3 | import club.yuit.oauth.boot.handler.BootLoginFailureHandler;
4 | import club.yuit.oauth.boot.support.BootSecurityProperties;
5 | import club.yuit.oauth.boot.support.BootSmsUserDetailService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.data.redis.core.StringRedisTemplate;
9 | import org.springframework.security.authentication.AuthenticationManager;
10 | import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
11 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
12 | import org.springframework.security.web.DefaultSecurityFilterChain;
13 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
14 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
15 |
16 | /**
17 | * @author yuit
18 | * @date 2019/11/26 9:27
19 | **/
20 | @Configuration
21 | public class SmsSecurityConfig extends SecurityConfigurerAdapter {
22 |
23 | private SmsAuthenticationProvider authenticationProvider;
24 | private SmsCodeAuthenticationFilter authenticationFilter;
25 | private SmsCodeCheckFilter codeCheckFilter;
26 |
27 |
28 | public SmsSecurityConfig(BootSmsUserDetailService userDetailsService,
29 | StringRedisTemplate redisTemplate,
30 | @Autowired(required = false)
31 | BootLoginFailureHandler failureHandler,
32 | @Autowired(required = false)
33 | AuthenticationSuccessHandler successHandler,
34 | BootSecurityProperties properties) {
35 |
36 |
37 | this.authenticationFilter = new SmsCodeAuthenticationFilter(properties.getSmsLogin().getLoginProcessUrl());
38 | this.authenticationFilter.setAuthenticationFailureHandler(failureHandler);
39 | if (successHandler!=null) {
40 | this.authenticationFilter.setAuthenticationSuccessHandler(successHandler);
41 | }
42 |
43 | this.authenticationProvider = new SmsAuthenticationProvider();
44 | this.authenticationProvider.setUserDetailsService(userDetailsService);
45 |
46 | this.codeCheckFilter = new SmsCodeCheckFilter(properties);
47 | this.codeCheckFilter.setFailureHandler(failureHandler);
48 | this.codeCheckFilter.setTemplate(redisTemplate);
49 | this.codeCheckFilter.setSuccessHandler(successHandler);
50 |
51 |
52 | }
53 |
54 | @Override
55 | public void configure(HttpSecurity http) throws Exception {
56 | this.authenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
57 | http.authenticationProvider(this.authenticationProvider)
58 | .addFilterBefore(this.codeCheckFilter,UsernamePasswordAuthenticationFilter.class)
59 | .addFilterAfter(this.authenticationFilter, UsernamePasswordAuthenticationFilter.class);
60 | }
61 |
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/CoreConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config;
2 |
3 | import club.yuit.oauth.boot.support.BootSecurityProperties;
4 | import club.yuit.oauth.boot.support.code.BootCodeService;
5 | import club.yuit.oauth.boot.support.code.RedisCodeService;
6 | import club.yuit.oauth.boot.support.code.SessionCodeService;
7 | import club.yuit.oauth.boot.support.properities.BootBaseLoginProperties;
8 | import club.yuit.oauth.boot.support.properities.CodeStoreType;
9 | import org.mybatis.spring.annotation.MapperScan;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.data.redis.core.StringRedisTemplate;
13 | import org.springframework.security.authentication.AuthenticationManager;
14 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
15 | import org.springframework.security.crypto.password.PasswordEncoder;
16 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
17 | import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
18 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
19 | import org.springframework.web.servlet.view.InternalResourceViewResolver;
20 | import springfox.documentation.builders.ApiInfoBuilder;
21 | import springfox.documentation.builders.ParameterBuilder;
22 | import springfox.documentation.builders.PathSelectors;
23 | import springfox.documentation.builders.RequestHandlerSelectors;
24 | import springfox.documentation.schema.ModelRef;
25 | import springfox.documentation.service.ApiInfo;
26 | import springfox.documentation.service.Contact;
27 | import springfox.documentation.service.Parameter;
28 | import springfox.documentation.spi.DocumentationType;
29 | import springfox.documentation.spring.web.plugins.Docket;
30 | import springfox.documentation.swagger2.annotations.EnableSwagger2;
31 |
32 | import java.util.ArrayList;
33 | import java.util.List;
34 |
35 | /**
36 | * @author yuit
37 | * @date 2018/10/9 15:08
38 | **/
39 | @Configuration
40 | @EnableSwagger2
41 | public class CoreConfig extends WebMvcConfigurationSupport {
42 |
43 |
44 |
45 | @Override
46 | public void addResourceHandlers(ResourceHandlerRegistry registry) {
47 | registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
48 | registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
49 | registry.addResourceHandler("/statics/**")
50 | .addResourceLocations("classpath:/statics/");
51 |
52 | registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/statics/favicon.ico");
53 | }
54 |
55 | /**
56 | * Could not resolve view with name 'forward:/oauth/confirm_access' in servlet with name 'dispatcherServlet'
57 | *
58 | */
59 | /*@Override
60 | protected void configureViewResolvers(ViewResolverRegistry registry) {
61 | registry.viewResolver(new InternalResourceViewResolver());
62 | }*/
63 |
64 | @Bean
65 | public Docket docket() {
66 |
67 | ParameterBuilder builder = new ParameterBuilder();
68 | List parameters = new ArrayList<>();
69 |
70 | builder.name("Authorization").description("token").modelRef(new ModelRef("string"))
71 | .parameterType("header")
72 | .required(false)
73 | .build();
74 | parameters.add(builder.build());
75 |
76 | return new Docket(DocumentationType.SWAGGER_2)
77 | .globalOperationParameters(parameters)
78 | .apiInfo(this.apiInfo())
79 | .select()
80 | .apis(RequestHandlerSelectors.basePackage("club.yuit.oauth.boot.controller"))
81 | .paths(PathSelectors.any())
82 | .build();
83 | }
84 |
85 | private ApiInfo apiInfo() {
86 | return new ApiInfoBuilder()
87 | .title("Boot API")
88 | .description("创建于 2018-10-12")
89 | .contact(new Contact("yuit", "", "1239964852g@gmail.com"))
90 | .version("1.0")
91 | .build();
92 | }
93 |
94 |
95 | @Bean
96 | public BootCodeService codeService(StringRedisTemplate template, BootSecurityProperties properties){
97 | if (properties.getCodeStoreType() == CodeStoreType.redis) {
98 | return new RedisCodeService(template,properties.getCodeExpireTime());
99 | }else {
100 | return new SessionCodeService();
101 | }
102 | }
103 |
104 |
105 |
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/DbConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import lombok.Data;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.boot.context.properties.ConfigurationProperties;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.context.annotation.Primary;
11 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
12 |
13 | import javax.sql.DataSource;
14 |
15 | /**
16 | * @author yuit
17 | * @date time 2018/10/9 15:14
18 | * 数据库配置
19 | **/
20 | @Configuration
21 | @ConfigurationProperties(prefix = "spring.datasource")
22 | @Data
23 | public class DbConfig {
24 |
25 | private String username;
26 | private String url;
27 | private String password;
28 | private String driverClassName;
29 |
30 | private Logger logger = LoggerFactory.getLogger(getClass());
31 |
32 |
33 | /**
34 | *
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
52 | *
53 | * */
54 | @Bean
55 | @Primary
56 | public DataSource dataSource() {
57 | DruidDataSource dr = new DruidDataSource();
58 | dr.setUsername(this.username);
59 | dr.setPassword(this.password);
60 | dr.setDriverClassName(this.driverClassName);
61 | dr.setUrl(this.url);
62 |
63 | this.logger.info(this.url+":"+this.username+":"+this.password);
64 |
65 | dr.setInitialSize(10);
66 | dr.setMinIdle(30);
67 | dr.setMaxActive(300);
68 | dr.setMaxWait(3600000);
69 | dr.setTimeBetweenEvictionRunsMillis(60000);
70 | dr.setMinEvictableIdleTimeMillis(30000);
71 | dr.setValidationQuery("select 'X' from dual");
72 | dr.setTestWhileIdle(true);
73 | dr.setTestOnBorrow(false);
74 | dr.setTestOnReturn(false);
75 | dr.setPoolPreparedStatements(true);
76 | dr.setMaxPoolPreparedStatementPerConnectionSize(20);
77 |
78 | return dr;
79 | }
80 |
81 | @Bean
82 | public DataSourceTransactionManager transactionManager(){
83 | return new DataSourceTransactionManager(dataSource());
84 | }
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config;
2 |
3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 | import com.fasterxml.jackson.annotation.PropertyAccessor;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import org.springframework.cache.CacheManager;
7 | import org.springframework.cache.annotation.EnableCaching;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.context.annotation.Primary;
11 | import org.springframework.data.redis.cache.RedisCacheManager;
12 | import org.springframework.data.redis.connection.RedisConnectionFactory;
13 | import org.springframework.data.redis.core.RedisTemplate;
14 | import org.springframework.data.redis.core.StringRedisTemplate;
15 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
16 | import org.springframework.data.redis.serializer.StringRedisSerializer;
17 |
18 | import javax.crypto.KeyGenerator;
19 | import java.lang.reflect.Method;
20 |
21 | /**
22 | * @author yuit
23 | * @date 2018/10/10 11:08
24 | *
25 | **/
26 | @Configuration
27 | @EnableCaching
28 | public class RedisConfig {
29 |
30 |
31 |
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/SecurityConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config;
2 |
3 | import club.yuit.oauth.boot.authentication.sms.SmsAuthenticationProvider;
4 | import club.yuit.oauth.boot.authentication.sms.SmsSecurityConfig;
5 | import club.yuit.oauth.boot.filter.BootPictureCodeAuthenticationFilter;
6 | import club.yuit.oauth.boot.handler.BootLoginFailureHandler;
7 | import club.yuit.oauth.boot.support.BootSecurityProperties;
8 | import club.yuit.oauth.boot.support.BootUserDetailService;
9 | import club.yuit.oauth.boot.support.code.BootCodeService;
10 | import club.yuit.oauth.boot.support.properities.BootBaseLoginProperties;
11 | import club.yuit.oauth.boot.support.properities.BootSmsLoginProperties;
12 | import org.springframework.context.annotation.Bean;
13 | import org.springframework.context.annotation.Configuration;
14 | import org.springframework.core.annotation.Order;
15 | import org.springframework.security.authentication.AuthenticationManager;
16 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
17 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
18 | import org.springframework.security.config.annotation.web.builders.WebSecurity;
19 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
20 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
21 | import org.springframework.security.crypto.password.PasswordEncoder;
22 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
23 |
24 | /**
25 | * @author yuit
26 | * @date 2018/10/10 11:48
27 | **/
28 | @Configuration
29 | @Order(1)
30 | public class SecurityConfig extends WebSecurityConfigurerAdapter {
31 |
32 |
33 | private BootUserDetailService userDetailService;
34 | private BootSecurityProperties properties;
35 | private BootLoginFailureHandler handler;
36 | private SmsSecurityConfig smsSecurityConfig;
37 | private BootPictureCodeAuthenticationFilter pictureCodeAuthenticationFilter;
38 |
39 |
40 | public SecurityConfig(BootUserDetailService userDetailService,
41 | BootSecurityProperties properties,
42 | BootLoginFailureHandler handler,
43 | SmsSecurityConfig smsSecurityConfig,
44 | BootCodeService codeService) {
45 | this.userDetailService = userDetailService;
46 | this.properties = properties;
47 | this.handler = handler;
48 | this.smsSecurityConfig = smsSecurityConfig;
49 | pictureCodeAuthenticationFilter = new BootPictureCodeAuthenticationFilter(properties,codeService, handler);
50 | }
51 |
52 | /**
53 | * 让Security 忽略这些url,不做拦截处理
54 | *
55 | * @param
56 | * @throws Exception
57 | */
58 | @Override
59 | public void configure(WebSecurity web) throws Exception {
60 | web.ignoring().antMatchers
61 | ("/swagger-ui.html/**", "/webjars/**",
62 | "/swagger-resources/**", "/v2/api-docs/**",
63 | "/swagger-resources/configuration/ui/**","/statics/**",properties.getCodePath(), "/swagger-resources/configuration/security/**",
64 | "/images/**");
65 | }
66 |
67 | @Override
68 | public void configure(AuthenticationManagerBuilder auth) throws Exception {
69 | auth.userDetailsService(userDetailService);
70 | }
71 |
72 | @Override
73 | protected void configure(HttpSecurity http) throws Exception {
74 |
75 | BootBaseLoginProperties base = properties.getBaseLogin();
76 | BootSmsLoginProperties sms = properties.getSmsLogin();
77 | http
78 | // http security 要拦截的url,这里这拦截,oauth2相关和登录登录相关的url,其他的交给资源服务处理
79 | .requestMatchers()
80 | .antMatchers( "/oauth/**",properties.getLoginPage(),
81 | base.getLoginProcessUrl(),sms.getLoginProcessUrl())
82 | .and()
83 | .authorizeRequests()
84 | // 自定义页面或处理url是,如果不配置全局允许,浏览器会提示服务器将页面转发多次
85 | .antMatchers(properties.getLoginPage(),base.getLoginProcessUrl(),sms.getLoginProcessUrl())
86 | .permitAll()
87 | .anyRequest()
88 | .authenticated();
89 |
90 | // 表单登录
91 | http.formLogin()
92 | .failureHandler(handler)
93 | // 请求 {用户名} 参数名称
94 | .usernameParameter(base.getUsernameParameterName())
95 | // 请求 {密码} 参数名
96 | .passwordParameter(base.getPasswordParameterName())
97 | // 登录页面
98 | .loginPage(properties.getLoginPage())
99 | // 登录处理url
100 | .loginProcessingUrl(base.getLoginProcessUrl());
101 |
102 | http.httpBasic().disable();
103 |
104 | http.apply(this.smsSecurityConfig);
105 |
106 | // 用户密码验证之前校验验证码
107 | http.addFilterBefore(pictureCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
108 |
109 | }
110 |
111 |
112 | @Override
113 | @Bean
114 | public AuthenticationManager authenticationManagerBean() throws Exception {
115 | return super.authenticationManagerBean();
116 | }
117 |
118 | @Bean
119 | public PasswordEncoder passwordEncoder() {
120 | return new BCryptPasswordEncoder();
121 | }
122 |
123 |
124 | @Bean
125 | public SmsAuthenticationProvider smsAuthenticationProvider (){
126 | return new SmsAuthenticationProvider();
127 | }
128 |
129 |
130 |
131 |
132 |
133 |
134 | }
135 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/auth2/OAuth2AuthorizationServerConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config.auth2;
2 |
3 | import club.yuit.oauth.boot.filter.BootBasicAuthenticationFilter;
4 | import club.yuit.oauth.boot.support.BootUserDetailService;
5 | import club.yuit.oauth.boot.support.oauth2.BootClientDetailsService;
6 | import club.yuit.oauth.boot.support.oauth2.BootOAuth2WebResponseExceptionTranslator;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.http.HttpMethod;
10 | import org.springframework.security.authentication.AuthenticationManager;
11 | import org.springframework.security.core.userdetails.UserDetailsService;
12 | import org.springframework.security.crypto.password.PasswordEncoder;
13 | import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
14 | import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
15 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
16 | import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
17 | import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
18 | import org.springframework.security.oauth2.provider.token.TokenStore;
19 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
20 | import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
21 | import org.springframework.security.web.AuthenticationEntryPoint;
22 |
23 | /**
24 | * @author yuit
25 | * @date 2018/10/15 14:52
26 | **/
27 | @Configuration
28 | @EnableAuthorizationServer
29 | public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
30 |
31 |
32 | private AuthenticationManager authenticationManager;
33 | private BootClientDetailsService clientDetailsService;
34 | private TokenStore tokenStore;
35 | private JwtAccessTokenConverter converter;
36 | private AuthenticationEntryPoint authenticationEntryPoint;
37 | private BootOAuth2WebResponseExceptionTranslator bootWebResponseExceptionTranslator;
38 | private PasswordEncoder passwordEncoder;
39 | private UserDetailsService userDetailsService;
40 |
41 | @Autowired(required = false)
42 | public OAuth2AuthorizationServerConfig(AuthenticationManager authenticationManager,
43 | BootClientDetailsService clientDetailsService,
44 | TokenStore tokenStore, JwtAccessTokenConverter converter,
45 | AuthenticationEntryPoint authenticationEntryPoint,
46 | BootOAuth2WebResponseExceptionTranslator bootWebResponseExceptionTranslator,
47 | PasswordEncoder passwordEncoder, BootUserDetailService userDetailsService) {
48 | this.authenticationManager = authenticationManager;
49 | this.clientDetailsService = clientDetailsService;
50 | this.tokenStore = tokenStore;
51 | this.converter = converter;
52 | this.authenticationEntryPoint = authenticationEntryPoint;
53 | this.bootWebResponseExceptionTranslator = bootWebResponseExceptionTranslator;
54 | this.passwordEncoder = passwordEncoder;
55 | this.userDetailsService = userDetailsService;
56 | }
57 |
58 | public OAuth2AuthorizationServerConfig() {
59 | super();
60 | }
61 |
62 | @Override
63 | public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
64 |
65 |
66 | // 允许表单登录
67 | security.allowFormAuthenticationForClients();
68 |
69 |
70 |
71 | // 自定义异常处理端口
72 | security.authenticationEntryPoint(authenticationEntryPoint);
73 |
74 | // 客户端认证之前的过滤器
75 | BootBasicAuthenticationFilter filter = new BootBasicAuthenticationFilter(this.clientDetailsService,this.passwordEncoder);
76 | security.addTokenEndpointAuthenticationFilter(filter);
77 |
78 | security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
79 |
80 | }
81 |
82 | @Override
83 | public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
84 | // 配置加载客户端的service
85 | clients.withClientDetails(clientDetailsService);
86 | }
87 |
88 | @Override
89 | public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
90 |
91 | endpoints
92 | // token 存储方式
93 | .tokenStore(tokenStore)
94 | .authenticationManager(authenticationManager)
95 | // 不配置会导致token无法刷新
96 | .userDetailsService(userDetailsService)
97 | .allowedTokenEndpointRequestMethods(HttpMethod.POST,HttpMethod.GET);
98 |
99 | // 判断当前是否使用jwt
100 | if(!(tokenStore instanceof RedisTokenStore) && this.converter!=null){
101 | endpoints.accessTokenConverter(converter);
102 | }
103 |
104 |
105 | // 处理 ExceptionTranslationFilter 抛出的异常
106 | endpoints.exceptionTranslator(bootWebResponseExceptionTranslator);
107 |
108 | endpoints.pathMapping("/oauth/confirm_access","/custom/confirm_access");
109 | }
110 |
111 | }
112 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/auth2/OAuth2ResourceServerConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config.auth2;
2 |
3 | import club.yuit.oauth.boot.support.BootSecurityProperties;
4 | import club.yuit.oauth.boot.support.oauth2.BootAccessDeniedHandler;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
8 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
9 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
10 | import org.springframework.security.oauth2.provider.token.TokenStore;
11 | import org.springframework.security.web.AuthenticationEntryPoint;
12 |
13 |
14 | /**
15 | * @author yuit
16 | * @date 2018/10/15 14:57
17 | * 资源服务配置
18 | **/
19 | @Configuration
20 | @EnableResourceServer
21 | public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter{
22 |
23 | private AuthenticationEntryPoint point;
24 | private BootAccessDeniedHandler handler;
25 | private TokenStore tokenStore;
26 | private BootSecurityProperties properties;
27 |
28 | public OAuth2ResourceServerConfig(AuthenticationEntryPoint point, BootAccessDeniedHandler handler, TokenStore tokenStore, BootSecurityProperties properties) {
29 | this.point = point;
30 | this.handler = handler;
31 | this.tokenStore = tokenStore;
32 | this.properties = properties;
33 | }
34 |
35 | @Override
36 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
37 |
38 | resources.tokenStore(tokenStore)
39 | .resourceId("boot-server");
40 |
41 | resources.authenticationEntryPoint(point).accessDeniedHandler(handler);
42 |
43 | }
44 |
45 | @Override
46 | public void configure(HttpSecurity http) throws Exception {
47 |
48 | http
49 | // Allows restricting access based upon the {@link HttpServletRequest} using
50 | .authorizeRequests()
51 | .antMatchers("/favicon.ico","/oauth/**",
52 | properties.getBaseLogin().getLoginProcessUrl(),
53 | properties.getLoginPage(),
54 | properties.getSmsLogin().getLoginProcessUrl())
55 | .permitAll()
56 | .anyRequest()
57 | .access("#oauth2.hasAnyScope('all','select')");
58 | }
59 |
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/config/auth2/TokenStoreConfig.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.config.auth2;
2 |
3 | import club.yuit.oauth.boot.support.BootSecurityProperties;
4 | import org.springframework.beans.factory.BeanCreationException;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.context.annotation.Primary;
10 | import org.springframework.data.redis.connection.RedisConnectionFactory;
11 | import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
12 | import org.springframework.security.oauth2.provider.token.TokenStore;
13 | import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
14 | import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
15 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
16 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
17 | import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
18 |
19 | import javax.sql.DataSource;
20 |
21 | /**
22 | * @author yuit
23 | * @date 2018/10/17 16:38
24 | *
25 | * token 存储方式配置
26 | *
27 | **/
28 | @Configuration
29 | public class TokenStoreConfig {
30 |
31 | private BootSecurityProperties properties;
32 |
33 | private RedisConnectionFactory factory;
34 |
35 |
36 |
37 | private DataSource dataSource;
38 |
39 | @Autowired(required = false)
40 | public TokenStoreConfig(BootSecurityProperties properties, RedisConnectionFactory factory, DataSource dataSource) {
41 | this.properties = properties;
42 | this.factory = factory;
43 |
44 | this.dataSource = dataSource;
45 | }
46 |
47 | @Bean
48 | public TokenStore tokenStore() throws Exception {
49 |
50 | TokenStore store = null;
51 |
52 | switch (properties.getOauth2().getTokenStoreType()) {
53 | case jwt:
54 | store = new JwtTokenStore(jwtAccessTokenConverter());
55 | break;
56 | case redis:
57 | if (factory == null) {
58 | throw new BeanCreationException("配置Redis存储Token需要redisConnectionFactory bean,未找到");
59 | }
60 | store = new RedisTokenStore(factory);
61 | break;
62 | case jdbc:
63 |
64 | if(dataSource==null){
65 | throw new BeanCreationException("配置jdbc存储Token需要dataSource bean,未找到");
66 | }
67 | store=new JdbcTokenStore(dataSource);
68 | break;
69 | default:
70 | store = new InMemoryTokenStore();
71 | }
72 |
73 | return store;
74 | }
75 |
76 | @Bean
77 | @Primary
78 | public JwtAccessTokenConverter jwtAccessTokenConverter() {
79 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
80 | converter.setSigningKey(properties.getOauth2().getTokenSigningKey());
81 | return converter;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/controller/BaseMainController.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.controller;
2 |
3 | import club.yuit.oauth.boot.response.HttpResponse;
4 | import club.yuit.oauth.boot.response.SimpleResponse;
5 | import club.yuit.oauth.boot.support.BootSecurityProperties;
6 | import club.yuit.oauth.boot.support.DefaultBeanName;
7 | import club.yuit.oauth.boot.support.code.BootCodeService;
8 | import club.yuit.oauth.boot.support.code.VerificationCode;
9 | import club.yuit.oauth.boot.support.code.VerificationCodeGenerator;
10 | import club.yuit.oauth.boot.support.code.picture.DefaultPictureCodeGenerator;
11 | import club.yuit.oauth.boot.support.code.sms.DefaultSmsCodeGenerator;
12 | import club.yuit.oauth.boot.support.properities.BootBaseLoginProperties;
13 | import club.yuit.oauth.boot.support.properities.CodeStoreType;
14 | import club.yuit.oauth.boot.utils.HttpUtils;
15 | import lombok.extern.slf4j.Slf4j;
16 | import org.bouncycastle.util.encoders.Base64;
17 | import org.springframework.beans.factory.annotation.Qualifier;
18 | import org.springframework.http.MediaType;
19 | import org.springframework.stereotype.Controller;
20 | import org.springframework.ui.Model;
21 | import org.springframework.web.bind.annotation.GetMapping;
22 | import org.springframework.web.bind.annotation.RequestMapping;
23 | import org.springframework.web.bind.annotation.RequestParam;
24 | import org.springframework.web.bind.annotation.ResponseBody;
25 |
26 | import javax.imageio.ImageIO;
27 | import javax.servlet.http.HttpServletRequest;
28 | import javax.servlet.http.HttpServletResponse;
29 | import java.awt.*;
30 | import java.awt.image.BufferedImage;
31 | import java.io.ByteArrayOutputStream;
32 | import java.io.IOException;
33 | import java.io.OutputStream;
34 | import java.io.PrintWriter;
35 | import java.util.HashMap;
36 | import java.util.Map;
37 | import java.util.UUID;
38 |
39 | /**
40 | * @author yuit
41 | * @date 2018/10/9 15:09
42 | **/
43 | @Controller
44 | @Slf4j
45 | public class BaseMainController {
46 |
47 |
48 | private BootSecurityProperties properties;
49 |
50 |
51 | public BaseMainController(BootSecurityProperties properties) {
52 | this.properties = properties;
53 | }
54 |
55 | @RequestMapping("${boot.oauth.login-page:/auth/login}")
56 | public String loginPage(Model model, HttpServletRequest request) {
57 | BootBaseLoginProperties base = properties.getBaseLogin();
58 | String type = request.getParameter("type");
59 |
60 | model.addAttribute("username", request.getParameter(base.getUsernameParameterName()));
61 | model.addAttribute("password", request.getParameter(base.getPasswordParameterName()));
62 | model.addAttribute("error", request.getAttribute("error"));
63 | if (type != null
64 | && (type.equalsIgnoreCase("base")
65 | || type.equalsIgnoreCase("sms")
66 | || type.equalsIgnoreCase("social"))) {
67 | model.addAttribute("type", type);
68 | } else {
69 | model.addAttribute("type", "base");
70 | }
71 |
72 | model.addAttribute("sms", properties.getSmsLogin());
73 | model.addAttribute("base", properties.getBaseLogin());
74 | model.addAttribute("codePath", properties.getCodePath());
75 | return "base-login";
76 | }
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/controller/BootGrantController.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.controller;
2 |
3 | import club.yuit.oauth.boot.entity.Client;
4 | import club.yuit.oauth.boot.service.IClientService;
5 | import club.yuit.oauth.boot.support.BootSecurityProperties;
6 | import org.springframework.security.oauth2.provider.AuthorizationRequest;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.ui.Model;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.SessionAttributes;
11 |
12 | import javax.servlet.http.HttpServletRequest;
13 | import java.util.Map;
14 |
15 | /**
16 | * @author yuit
17 | * @date 2018/11/1 11:44
18 | *
19 | */
20 | @Controller
21 | @SessionAttributes("authorizationRequest")
22 | public class BootGrantController {
23 |
24 | private BootSecurityProperties properties;
25 | private IClientService clientService;
26 |
27 | public BootGrantController(BootSecurityProperties properties, IClientService clientService) {
28 | this.properties = properties;
29 | this.clientService = clientService;
30 | }
31 |
32 | @RequestMapping("/custom/confirm_access")
33 | public String getAccessConfirmation(Map param, HttpServletRequest request, Model model) throws Exception {
34 |
35 | AuthorizationRequest authorizationRequest = (AuthorizationRequest) param.get("authorizationRequest");
36 | if (authorizationRequest==null){
37 | return "redirect:"+properties.getLoginPage();
38 | }
39 | String clientId = authorizationRequest.getClientId();
40 | model.addAttribute("scopes",authorizationRequest.getScope());
41 | Client client = this.clientService.findClientByClientId(clientId);
42 | model.addAttribute("client",client);
43 |
44 | return "base-grant";
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/controller/ClientController.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.controller;
2 |
3 | import club.yuit.oauth.boot.entity.Client;
4 | import club.yuit.oauth.boot.response.BaseResponse;
5 | import club.yuit.oauth.boot.response.HttpResponse;
6 | import club.yuit.oauth.boot.service.IClientService;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.security.crypto.password.PasswordEncoder;
9 | import org.springframework.web.bind.annotation.PostMapping;
10 | import org.springframework.web.bind.annotation.RequestBody;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import javax.validation.Valid;
15 |
16 | /**
17 | * @author yuit
18 | * @date 2018/10/16 10:32
19 | *
20 | **/
21 | @RestController
22 | @RequestMapping("client")
23 | public class ClientController {
24 |
25 | @Autowired
26 | private IClientService clientService;
27 |
28 | @Autowired
29 | private PasswordEncoder passwordEncoder;
30 |
31 | @PostMapping("/register")
32 | public BaseResponse clientRegistered(@RequestBody @Valid Client client) {
33 |
34 | client.setClientSecret(passwordEncoder.encode(client.getClientSecret()));
35 | boolean i = clientService.save(client);
36 | return HttpResponse.baseResponse(200);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/club/yuit/oauth/boot/controller/CodeController.java:
--------------------------------------------------------------------------------
1 | package club.yuit.oauth.boot.controller;
2 |
3 | import club.yuit.oauth.boot.response.HttpResponse;
4 | import club.yuit.oauth.boot.response.SimpleResponse;
5 | import club.yuit.oauth.boot.support.BootSecurityProperties;
6 | import club.yuit.oauth.boot.support.DefaultBeanName;
7 | import club.yuit.oauth.boot.support.code.BootCodeService;
8 | import club.yuit.oauth.boot.support.code.VerificationCode;
9 | import club.yuit.oauth.boot.support.code.VerificationCodeGenerator;
10 | import club.yuit.oauth.boot.support.code.picture.DefaultPictureCodeGenerator;
11 | import club.yuit.oauth.boot.support.code.sms.DefaultSmsCodeGenerator;
12 | import club.yuit.oauth.boot.support.properities.CodeStoreType;
13 | import club.yuit.oauth.boot.utils.HttpUtils;
14 | import jdk.nashorn.internal.objects.annotations.Getter;
15 | import org.bouncycastle.util.encoders.Base64;
16 | import org.springframework.http.MediaType;
17 | import org.springframework.stereotype.Controller;
18 | import org.springframework.ui.Model;
19 | import org.springframework.web.bind.annotation.GetMapping;
20 | import org.springframework.web.bind.annotation.RequestMapping;
21 | import org.springframework.web.bind.annotation.RequestParam;
22 | import org.springframework.web.bind.annotation.ResponseBody;
23 |
24 | import javax.imageio.ImageIO;
25 | import javax.servlet.http.Cookie;
26 | import javax.servlet.http.HttpServletRequest;
27 | import javax.servlet.http.HttpServletResponse;
28 | import java.awt.image.BufferedImage;
29 | import java.io.ByteArrayOutputStream;
30 | import java.io.OutputStream;
31 | import java.util.HashMap;
32 | import java.util.Map;
33 | import java.util.UUID;
34 |
35 | /**
36 | * @author yuit
37 | * @date 2019/11/27 9:27
38 | **/
39 | @Controller
40 | @RequestMapping("${boot.oauth.code-path:/auth/code}")
41 | public class CodeController {
42 |
43 |
44 | private BootSecurityProperties properties;
45 | private Map codeGenerators;
46 |
47 | public CodeController(BootSecurityProperties properties, Map codeGenerators) {
48 | this.properties = properties;
49 | this.codeGenerators = codeGenerators;
50 | }
51 |
52 | /**
53 | *
54 | * @param request request
55 | * @param type sms/picture
56 | * @throws Exception ex
57 | */
58 | @RequestMapping()
59 | @ResponseBody
60 | public SimpleResponse