├── 2_jwt_token ├── test │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── TestApplication.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ ├── RootUtil.java │ │ │ └── HttpUtil.java │ └── pom.xml ├── resource_server │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.yaml │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── ResourceServerApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ ├── TokenConfig.java │ │ │ │ └── ResourceServerConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── ResourceServerApplicationTests.java │ └── pom.xml └── authorization_server │ ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yaml │ │ └── java │ │ │ └── lee │ │ │ ├── AuthorizationServerApplication.java │ │ │ ├── config │ │ │ ├── TokenConfig.java │ │ │ └── SecurityConfig.java │ │ │ └── model │ │ │ └── MyUserDetails.java │ └── test │ │ └── java │ │ └── lee │ │ └── AuthorizationServerApplicationTests.java │ └── pom.xml ├── 1_simple_token ├── test │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── TestApplication.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ ├── RootUtil.java │ │ │ └── HttpUtil.java │ └── pom.xml ├── resource_server │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.yaml │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── ResourceServerApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ └── ResourceServerConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── ResourceServerApplicationTests.java │ └── pom.xml └── authorization_server │ └── src │ ├── main │ ├── resources │ │ └── application.yaml │ └── java │ │ └── lee │ │ ├── AuthorizationServerApplication.java │ │ ├── config │ │ ├── TokenConfig.java │ │ └── SecurityConfig.java │ │ └── model │ │ └── MyUserDetails.java │ └── test │ └── java │ └── lee │ └── AuthorizationServerApplicationTests.java ├── 3_store_in_db ├── test │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── TestApplication.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ ├── RootUtil.java │ │ │ └── HttpUtil.java │ └── pom.xml ├── resource_server │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.yaml │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── ResourceServerApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ └── ResourceServerConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── ResourceServerApplicationTests.java │ └── pom.xml └── authorization_server │ └── src │ ├── main │ ├── resources │ │ ├── application.yaml │ │ └── application.properties │ └── java │ │ └── lee │ │ ├── AuthorizationServerApplication.java │ │ ├── config │ │ ├── TokenConfig.java │ │ └── SecurityConfig.java │ │ └── model │ │ └── MyUserDetails.java │ └── test │ └── java │ └── lee │ └── AuthorizationServerApplicationTests.java ├── 4_spring_cloud ├── test │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── TestApplication.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ ├── RootUtil.java │ │ │ └── HttpUtil.java │ └── pom.xml ├── zuul │ └── src │ │ ├── main │ │ ├── resources │ │ │ ├── application.properties │ │ │ └── application.yaml │ │ └── java │ │ │ └── lee │ │ │ └── ZuulApplication.java │ │ └── test │ │ └── java │ │ └── lee │ │ └── ZuulApplicationTests.java ├── eureka │ ├── src │ │ ├── test │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── EurekaApplicationTests.java │ │ └── main │ │ │ ├── java │ │ │ └── lee │ │ │ │ └── EurekaApplication.java │ │ │ └── resources │ │ │ └── application.yaml │ └── pom.xml ├── resource_server_1 │ └── src │ │ ├── test │ │ └── java │ │ │ └── lee │ │ │ └── ResourceServerApplicationTests.java │ │ └── main │ │ ├── resources │ │ └── application.yaml │ │ └── java │ │ └── lee │ │ ├── ResourceServerApplication.java │ │ ├── controller │ │ └── IndexController.java │ │ └── config │ │ ├── SecurityConfig.java │ │ ├── TokenConfig.java │ │ └── ResourceServerConfig.java ├── resource_server_2 │ └── src │ │ ├── test │ │ └── java │ │ │ └── lee │ │ │ └── ResourceServerApplicationTests.java │ │ └── main │ │ ├── resources │ │ └── application.yaml │ │ └── java │ │ └── lee │ │ ├── ResourceServerApplication.java │ │ ├── controller │ │ └── IndexController.java │ │ └── config │ │ ├── SecurityConfig.java │ │ ├── TokenConfig.java │ │ └── ResourceServerConfig.java └── authorization_server │ └── src │ ├── test │ └── java │ │ └── lee │ │ └── AuthorizationServerApplicationTests.java │ └── main │ ├── resources │ └── application.yaml │ └── java │ └── lee │ ├── AuthorizationServerApplication.java │ ├── config │ ├── TokenConfig.java │ └── SecurityConfig.java │ └── model │ └── MyUserDetails.java ├── video_code ├── 1.simple_token │ ├── test │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── TestApplication.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── RootUtil.java │ │ │ │ └── HttpUtil.java │ │ └── pom.xml │ ├── auth │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ ├── AuthApplication.java │ │ │ │ │ ├── config │ │ │ │ │ └── TokenConfig.java │ │ │ │ │ └── model │ │ │ │ │ └── MyUserDetails.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── AuthApplicationTests.java │ │ └── pom.xml │ └── source │ │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── SourceApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ └── SourceConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── SourceApplicationTests.java │ │ └── pom.xml ├── 2.jwt_token │ ├── test │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── TestApplication.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── RootUtil.java │ │ │ │ ├── HttpUtil.java │ │ │ │ └── TT.java │ │ └── pom.xml │ ├── auth │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ ├── AuthApplication.java │ │ │ │ │ ├── config │ │ │ │ │ └── TokenConfig.java │ │ │ │ │ └── model │ │ │ │ │ └── MyUserDetails.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── AuthApplicationTests.java │ │ └── pom.xml │ └── source │ │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── SourceApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ ├── TokenConfig.java │ │ │ │ └── SourceConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── SourceApplicationTests.java │ │ └── pom.xml ├── 3.store_in_db │ ├── test │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── TestApplication.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── RootUtil.java │ │ │ │ └── HttpUtil.java │ │ └── pom.xml │ ├── source │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ ├── SourceApplication.java │ │ │ │ │ ├── controller │ │ │ │ │ └── IndexController.java │ │ │ │ │ └── config │ │ │ │ │ ├── SecurityConfig.java │ │ │ │ │ └── SourceConfig.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── SourceApplicationTests.java │ │ └── pom.xml │ └── auth │ │ └── src │ │ ├── test │ │ └── java │ │ │ └── lee │ │ │ ├── AuthApplicationTests.java │ │ │ └── TT.java │ │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── lee │ │ ├── AuthApplication.java │ │ ├── config │ │ └── TokenConfig.java │ │ └── model │ │ └── MyUserDetails.java ├── 5.spring_cloud │ ├── test │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.properties │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── TestApplication.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── RootUtil.java │ │ │ │ ├── HttpUtil.java │ │ │ │ ├── testClient1.java │ │ │ │ └── testClient2.java │ │ └── pom.xml │ ├── eureka │ │ ├── src │ │ │ ├── main │ │ │ │ ├── resources │ │ │ │ │ └── application.yaml │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── EurekaApplication.java │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── EurekaApplicationTests.java │ │ └── pom.xml │ ├── zuul │ │ └── src │ │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.properties │ │ │ │ └── application.yaml │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── ZuulApplication.java │ │ │ │ └── filter │ │ │ │ └── ScopeFilter.java │ │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── ZuulApplicationTests.java │ ├── auth │ │ ├── src │ │ │ ├── test │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── AuthApplicationTests.java │ │ │ └── main │ │ │ │ ├── resources │ │ │ │ └── application.yaml │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── AuthApplication.java │ │ │ │ ├── config │ │ │ │ └── TokenConfig.java │ │ │ │ └── model │ │ │ │ └── MyUserDetails.java │ │ └── pom.xml │ ├── source1 │ │ ├── src │ │ │ ├── test │ │ │ │ └── java │ │ │ │ │ └── lee │ │ │ │ │ └── SourceApplicationTests.java │ │ │ └── main │ │ │ │ ├── resources │ │ │ │ └── application.yaml │ │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── SourceApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ ├── TokenConfig.java │ │ │ │ └── SourceConfig.java │ │ └── pom.xml │ └── source2 │ │ ├── src │ │ ├── test │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── SourceApplicationTests.java │ │ └── main │ │ │ ├── resources │ │ │ └── application.yaml │ │ │ └── java │ │ │ └── lee │ │ │ ├── SourceApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── config │ │ │ ├── SecurityConfig.java │ │ │ ├── TokenConfig.java │ │ │ └── SourceConfig.java │ │ └── pom.xml └── 4.jwt_store_in_db │ ├── test │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ └── TestApplication.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ ├── RootUtil.java │ │ │ └── HttpUtil.java │ └── pom.xml │ ├── source │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── lee │ │ │ │ ├── SourceApplication.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ ├── SecurityConfig.java │ │ │ │ ├── TokenConfig.java │ │ │ │ └── SourceConfig.java │ │ └── test │ │ │ └── java │ │ │ └── lee │ │ │ └── SourceApplicationTests.java │ └── pom.xml │ └── auth │ └── src │ ├── test │ └── java │ │ └── lee │ │ ├── AuthApplicationTests.java │ │ └── TT.java │ └── main │ ├── resources │ └── application.properties │ └── java │ └── lee │ ├── AuthApplication.java │ ├── config │ └── TokenConfig.java │ └── model │ └── MyUserDetails.java ├── 图.rp └── README.md /2_jwt_token/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /1_simple_token/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /3_store_in_db/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /4_spring_cloud/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /video_code/1.simple_token/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /图.rp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lansinuote/Spring-Oauth2-Toturials/HEAD/图.rp -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4001 -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=3001 -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=3001 -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=4001 -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4001 -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3001 -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4001 -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=4001 -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=4001 -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=4001 -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3001 -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3001 -------------------------------------------------------------------------------- /4_spring_cloud/zuul/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lansinuote/Spring-Oauth2-Toturials/HEAD/4_spring_cloud/zuul/src/main/resources/application.properties -------------------------------------------------------------------------------- /video_code/5.spring_cloud/eureka/src/main/resources/application.yaml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lansinuote/Spring-Oauth2-Toturials/HEAD/video_code/5.spring_cloud/eureka/src/main/resources/application.yaml -------------------------------------------------------------------------------- /video_code/5.spring_cloud/zuul/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lansinuote/Spring-Oauth2-Toturials/HEAD/video_code/5.spring_cloud/zuul/src/main/resources/application.properties -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://server50:3306/oauth?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /4_spring_cloud/zuul/src/test/java/lee/ZuulApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ZuulApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /4_spring_cloud/eureka/src/test/java/lee/EurekaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EurekaApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/src/test/java/lee/AuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/src/test/java/lee/AuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/test/java/lee/AuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/src/test/java/lee/AuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/zuul/src/test/java/lee/ZuulApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ZuulApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/test/java/lee/ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ResourceServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | } -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/test/java/lee/AuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/eureka/src/test/java/lee/EurekaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EurekaApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/test/java/lee/ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ResourceServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | } -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/test/java/lee/ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ResourceServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/test/java/lee/ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ResourceServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/test/java/lee/ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ResourceServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/test/java/lee/SourceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SourceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=3001 2 | 3 | spring.datasource.url=jdbc:mysql://192.168.56.101:3306/oauth2?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=3001 2 | 3 | spring.datasource.url=jdbc:mysql://192.168.56.101:3306/oauth2?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/test/java/lee/AuthorizationServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthorizationServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/test/java/lee/AuthorizationServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthorizationServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/test/java/lee/AuthorizationServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthorizationServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/test/java/lee/AuthorizationServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class AuthorizationServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/test/java/lee/TT.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 5 | 6 | public class TT { 7 | 8 | @Test 9 | public void testPass(){ 10 | String encode = new BCryptPasswordEncoder().encode("123123"); 11 | System.out.println(encode); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/test/java/lee/TT.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 5 | 6 | public class TT { 7 | 8 | @Test 9 | public void testPass(){ 10 | String encode = new BCryptPasswordEncoder().encode("123123"); 11 | System.out.println(encode); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /1_simple_token/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /2_jwt_token/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /3_store_in_db/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /4_spring_cloud/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/src/main/java/lee/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/main/java/lee/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/src/main/java/lee/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/1.simple_token/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/main/java/lee/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/main/java/lee/TestApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SourceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SourceApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SourceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SourceApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SourceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SourceApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SourceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SourceApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/main/java/lee/ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ResourceServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ResourceServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/java/lee/ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ResourceServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ResourceServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/main/java/lee/ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ResourceServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ResourceServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/main/java/lee/AuthorizationServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthorizationServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthorizationServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/main/java/lee/AuthorizationServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthorizationServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthorizationServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/java/lee/AuthorizationServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class AuthorizationServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(AuthorizationServerApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /4_spring_cloud/zuul/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 5001 3 | spring: 4 | application: 5 | name: zuul 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /4_spring_cloud/zuul/src/main/java/lee/ZuulApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.cloud.client.SpringCloudApplication; 5 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 | 7 | @SpringCloudApplication 8 | @EnableZuulProxy 9 | public class ZuulApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ZuulApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3001 3 | spring: 4 | application: 5 | name: auth 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /video_code/5.spring_cloud/zuul/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 5001 3 | spring: 4 | application: 5 | name: zuul 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4001 3 | spring: 4 | application: 5 | name: source1 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4002 3 | spring: 4 | application: 5 | name: source2 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4001 3 | spring: 4 | application: 5 | name: resource_server1 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 4002 3 | spring: 4 | application: 5 | name: resource_server2 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /video_code/5.spring_cloud/zuul/src/main/java/lee/ZuulApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.cloud.client.SpringCloudApplication; 5 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 | 7 | @SpringCloudApplication 8 | @EnableZuulProxy 9 | public class ZuulApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ZuulApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3001 3 | spring: 4 | application: 5 | name: authorization_server 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | prefer-ip-address: false 12 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 13 | hostname: ${spring.cloud.client.ipAddress} 14 | client: 15 | serviceUrl: 16 | #eurekaServers 17 | defaultZone: http://127.0.0.1:2001/eureka -------------------------------------------------------------------------------- /4_spring_cloud/eureka/src/main/java/lee/EurekaApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class EurekaApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/eureka/src/main/java/lee/EurekaApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class EurekaApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/src/main/java/lee/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class AuthApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(AuthApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class SourceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SourceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/java/lee/SourceApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class SourceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SourceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/java/lee/ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class ResourceServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ResourceServerApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/java/lee/ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class ResourceServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ResourceServerApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; 7 | 8 | @Configuration 9 | public class TokenConfig { 10 | 11 | @Bean 12 | public TokenStore tokenStore() { 13 | return new InMemoryTokenStore(); 14 | } 15 | } -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/main/java/lee/AuthorizationServerApplication.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class AuthorizationServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(AuthorizationServerApplication.class, args); 13 | } 14 | } -------------------------------------------------------------------------------- /4_spring_cloud/eureka/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2001 3 | spring: 4 | application: 5 | name: eureka 6 | cloud: 7 | client: 8 | ipAddress: 127.0.0.1 9 | eureka: 10 | instance: 11 | #基于ip配置 12 | prefer-ip-address: false 13 | #自定义id 14 | instance-id: ${spring.cloud.client.ipAddress}:${server.port} 15 | hostname: ${spring.cloud.client.ipAddress} 16 | client: 17 | serviceUrl: 18 | defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 19 | #注册到其他eureka 20 | registerWithEureka: false 21 | #从其他eureka拉取信息 22 | fetchRegistry: false 23 | server: 24 | #自我保护 25 | enable-self-preservation: false -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.oauth2.provider.token.TokenStore; 7 | import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; 8 | 9 | import javax.sql.DataSource; 10 | 11 | @Configuration 12 | public class TokenConfig { 13 | 14 | @Autowired 15 | private DataSource dataSource; 16 | 17 | @Bean 18 | public TokenStore tokenStore() { 19 | return new JdbcTokenStore(dataSource); 20 | } 21 | } -------------------------------------------------------------------------------- /2_jwt_token/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /1_simple_token/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /3_store_in_db/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /4_spring_cloud/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /video_code/1.simple_token/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/test/src/test/java/lee/RootUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class RootUtil { 9 | 10 | public static Map buildMap(String... args) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < args.length; i += 2) { 13 | map.put(args[i], args[i + 1]); 14 | } 15 | return map; 16 | } 17 | 18 | public static JSONObject buildJson(String... args) { 19 | JSONObject jsonObject = new JSONObject(); 20 | for (int i = 0; i < args.length; i += 2) { 21 | jsonObject.put(args[i], args[i + 1]); 22 | } 23 | return jsonObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.oauth2.provider.token.TokenStore; 7 | import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; 8 | 9 | import javax.sql.DataSource; 10 | 11 | @Configuration 12 | public class TokenConfig { 13 | 14 | @Autowired 15 | private DataSource dataSource; 16 | 17 | //配置token的存储方法 18 | @Bean 19 | public TokenStore tokenStore() { 20 | //配置token存储在内存中,这种是普通token,每次都需要远程校验,性能较差 21 | return new JdbcTokenStore(dataSource); 22 | } 23 | } -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | @RequestMapping("admin") 18 | @PreAuthorize("hasAnyAuthority('admin')") 19 | public String admin() { 20 | return "admin"; 21 | } 22 | 23 | @RequestMapping("me") 24 | public Principal me(Principal principal) { 25 | return principal; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | //测试接口 18 | @RequestMapping("admin") 19 | @PreAuthorize("hasAnyAuthority('admin')") 20 | public String admin() { 21 | return "admin"; 22 | } 23 | 24 | @RequestMapping("me") 25 | public Principal me(Principal principal) { 26 | return principal; 27 | } 28 | } -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | //测试接口 18 | @RequestMapping("admin") 19 | @PreAuthorize("hasAnyAuthority('admin')") 20 | public String admin() { 21 | return "admin"; 22 | } 23 | 24 | @RequestMapping("me") 25 | public Principal me(Principal principal) { 26 | return principal; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | //测试接口 18 | @RequestMapping("admin") 19 | @PreAuthorize("hasAnyAuthority('admin')") 20 | public String admin() { 21 | return "admin"; 22 | } 23 | 24 | @RequestMapping("me") 25 | public Principal me(Principal principal) { 26 | return principal; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | //测试接口 18 | @RequestMapping("admin") 19 | @PreAuthorize("hasAnyAuthority('admin')") 20 | public String admin() { 21 | return "admin"; 22 | } 23 | 24 | @RequestMapping("me") 25 | public Principal me(Principal principal) { 26 | return principal; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/java/lee/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package lee.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.security.Principal; 8 | 9 | @RestController 10 | public class IndexController { 11 | 12 | @RequestMapping("user") 13 | public String user() { 14 | return "user"; 15 | } 16 | 17 | //测试接口 18 | @RequestMapping("admin") 19 | @PreAuthorize("hasAnyAuthority('admin')") 20 | public String admin() { 21 | return "admin"; 22 | } 23 | 24 | @RequestMapping("me") 25 | public Principal me(Principal principal) { 26 | return principal; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import ch.qos.logback.core.rolling.helper.TokenConverter; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.oauth2.provider.token.TokenStore; 7 | import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; 8 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 9 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 10 | 11 | @Configuration 12 | public class TokenConfig { 13 | 14 | //配置token的存储方法 15 | @Bean 16 | public TokenStore tokenStore() { 17 | //配置token存储在内存中,这种是普通token,每次都需要远程校验,性能较差 18 | return new InMemoryTokenStore(); 19 | } 20 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 10 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 11 | 12 | @Override 13 | protected void configure(HttpSecurity http) throws Exception { 14 | http.csrf().disable() 15 | .authorizeRequests() 16 | .anyRequest().permitAll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | 16 | converter.setSigningKey("jwt_key"); 17 | return converter; 18 | } 19 | 20 | @Bean 21 | public TokenStore tokenStore() { 22 | return new JwtTokenStore(tokenConverter()); 23 | } 24 | } -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | 16 | converter.setSigningKey("jwt_key"); 17 | return converter; 18 | } 19 | 20 | @Bean 21 | public TokenStore tokenStore() { 22 | return new JwtTokenStore(tokenConverter()); 23 | } 24 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | converter.setSigningKey("jwt_key"); 16 | return converter; 17 | } 18 | 19 | @Bean 20 | public TokenStore tokenStore() { 21 | return new JwtTokenStore(tokenConverter()); 22 | } 23 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | 16 | converter.setSigningKey("jwt_key"); 17 | return converter; 18 | } 19 | 20 | @Bean 21 | public TokenStore tokenStore() { 22 | return new JwtTokenStore(tokenConverter()); 23 | } 24 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | converter.setSigningKey("jwt_key"); 16 | return converter; 17 | } 18 | 19 | @Bean 20 | public TokenStore tokenStore() { 21 | return new JwtTokenStore(tokenConverter()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | 16 | converter.setSigningKey("jwt_key"); 17 | return converter; 18 | } 19 | 20 | @Bean 21 | public TokenStore tokenStore() { 22 | return new JwtTokenStore(tokenConverter()); 23 | } 24 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | @Bean 13 | public JwtAccessTokenConverter tokenConverter() { 14 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 15 | 16 | converter.setSigningKey("jwt_key"); 17 | return converter; 18 | } 19 | 20 | @Bean 21 | public TokenStore tokenStore() { 22 | return new JwtTokenStore(tokenConverter()); 23 | } 24 | } -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | //配置如何把普通token转换成jwt token 13 | @Bean 14 | public JwtAccessTokenConverter tokenConverter() { 15 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 16 | 17 | //使用对称秘钥加密token,resource那边会用这个秘钥校验token 18 | converter.setSigningKey("uaa123"); 19 | return converter; 20 | } 21 | 22 | //配置token的存储方法 23 | @Bean 24 | public TokenStore tokenStore() { 25 | //把用户信息都存储在token当中,相当于存储在客户端,性能好很多 26 | return new JwtTokenStore(tokenConverter()); 27 | } 28 | } -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | //配置如何把普通token转换成jwt token 13 | @Bean 14 | public JwtAccessTokenConverter tokenConverter() { 15 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 16 | 17 | //使用对称秘钥加密token,resource那边会用这个秘钥校验token 18 | converter.setSigningKey("uaa123"); 19 | return converter; 20 | } 21 | 22 | //配置token的存储方法 23 | @Bean 24 | public TokenStore tokenStore() { 25 | //把用户信息都存储在token当中,相当于存储在客户端,性能好很多 26 | return new JwtTokenStore(tokenConverter()); 27 | } 28 | } -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | //配置如何把普通token转换成jwt token 13 | @Bean 14 | public JwtAccessTokenConverter tokenConverter() { 15 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 16 | 17 | //使用对称秘钥加密token,resource那边会用这个秘钥校验token 18 | converter.setSigningKey("uaa123"); 19 | return converter; 20 | } 21 | 22 | //配置token的存储方法 23 | @Bean 24 | public TokenStore tokenStore() { 25 | //把用户信息都存储在token当中,相当于存储在客户端,性能好很多 26 | return new JwtTokenStore(tokenConverter()); 27 | } 28 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | //配置如何把普通token转换成jwt token 13 | @Bean 14 | public JwtAccessTokenConverter tokenConverter() { 15 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 16 | 17 | //使用对称秘钥加密token,resource那边会用这个秘钥校验token 18 | converter.setSigningKey("uaa123"); 19 | return converter; 20 | } 21 | 22 | //配置token的存储方法 23 | @Bean 24 | public TokenStore tokenStore() { 25 | //把用户信息都存储在token当中,相当于存储在客户端,性能好很多 26 | return new JwtTokenStore(tokenConverter()); 27 | } 28 | } -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/java/lee/config/TokenConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.oauth2.provider.token.TokenStore; 6 | import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; 7 | import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; 8 | 9 | @Configuration 10 | public class TokenConfig { 11 | 12 | //配置如何把普通token转换成jwt token 13 | @Bean 14 | public JwtAccessTokenConverter tokenConverter() { 15 | JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 16 | 17 | //使用对称秘钥加密token,resource那边会用这个秘钥校验token 18 | converter.setSigningKey("uaa123"); 19 | return converter; 20 | } 21 | 22 | //配置token的存储方法 23 | @Bean 24 | public TokenStore tokenStore() { 25 | //把用户信息都存储在token当中,相当于存储在客户端,性能好很多 26 | return new JwtTokenStore(tokenConverter()); 27 | } 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 最简单的基于Spring Boot/Cloud使用Oauth2分布式鉴权教程. 2 | 3 | 代码我尽量给出了最简单的形式,只需要你有最基础的Spring Boot基础就能看懂. 4 | 5 | 新手请按照文件夹顺序一个一个的看,authorization_server是授权服务,resource_server是资源服务,使用test项目中的用例执行测试 6 | 7 | 每个项目中的文件都非常少,请阅读代码中的注释,相信你一定能看得懂. 8 | 9 | ------------------------------------------- 10 | 11 | 大致解释一下本项目的运行方法. 12 | 13 | 这里以simple_token为例子来讲解. 14 | 15 | 下载项目并解压之后得到文件夹Spring-Oauth2-Toturials-master 16 | 17 | 打开intellij,引入项目Spring-Oauth2-Toturials-master\1_simple_token\authorization_server 18 | 19 | 运行成功无异常后,使用intellij的File-Open打开项目Spring-Oauth2-Toturials-master\1_simple_token\resource_server 20 | 21 | 这时你应该有两个intellij窗口,分别运行一个项目. 22 | 23 | 确认两个项目都运行无异常后,再次使用intellij的File-Open打开项目Spring-Oauth2-Toturials-master\1_simple_token\test 24 | 25 | test项目不需要运行,它不是一个服务,你可以在test项目中的src\test\java\lee\TT.java中找到单元测试.然后跟着测试类里的注释,一个一个地跑单元测试就行了. 26 | 27 | 祝学习顺利. 28 | 29 | ------------------------------------------- 30 | 31 | 视频课程已发布 32 | 33 | 1.最简单的使用SpringOAuth2进行分布式权限管理:https://www.bilibili.com/video/BV1p5411N7Po 34 | 35 | 2.在SpringOAuth2中使用JwtToken和数据库持久化:https://www.bilibili.com/video/BV1Py4y1a7Nu 36 | 37 | 3.在SpringCloud微服务架构中使用SpringOAuth2(完结):https://www.bilibili.com/video/BV1Ky4y1e7JJ 38 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | @EnableResourceServer 14 | public class SourceConfig extends ResourceServerConfigurerAdapter { 15 | 16 | @Autowired 17 | private TokenStore tokenStore; 18 | 19 | @Override 20 | public void configure(ResourceServerSecurityConfigurer resources) { 21 | 22 | resources.resourceId("resource1") 23 | .tokenStore(tokenStore) 24 | .stateless(true); 25 | } 26 | 27 | @Override 28 | public void configure(HttpSecurity http) throws Exception { 29 | http.csrf().disable() 30 | .authorizeRequests() 31 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 32 | 33 | .and() 34 | 35 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | @EnableResourceServer 14 | public class SourceConfig extends ResourceServerConfigurerAdapter { 15 | 16 | @Autowired 17 | private TokenStore tokenStore; 18 | 19 | @Override 20 | public void configure(ResourceServerSecurityConfigurer resources) { 21 | 22 | resources.resourceId("resource1") 23 | .tokenStore(tokenStore) 24 | .stateless(true); 25 | } 26 | 27 | @Override 28 | public void configure(HttpSecurity http) throws Exception { 29 | http.csrf().disable() 30 | .authorizeRequests() 31 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 32 | 33 | .and() 34 | 35 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | @EnableResourceServer 14 | public class SourceConfig extends ResourceServerConfigurerAdapter { 15 | 16 | @Autowired 17 | private TokenStore tokenStore; 18 | 19 | @Override 20 | public void configure(ResourceServerSecurityConfigurer resources) { 21 | 22 | resources.resourceId("resource2") 23 | .tokenStore(tokenStore) 24 | .stateless(true); 25 | } 26 | 27 | @Override 28 | public void configure(HttpSecurity http) throws Exception { 29 | http.csrf().disable() 30 | .authorizeRequests() 31 | .antMatchers("/**").access("#oauth2.hasScope('scope2')") 32 | 33 | .and() 34 | 35 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | @EnableResourceServer 14 | public class SourceConfig extends ResourceServerConfigurerAdapter { 15 | 16 | @Autowired 17 | private TokenStore tokenStore; 18 | 19 | @Override 20 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 21 | 22 | resources.resourceId("resource1") 23 | .tokenStore(tokenStore) 24 | .stateless(true); 25 | } 26 | 27 | @Override 28 | public void configure(HttpSecurity http) throws Exception { 29 | http.csrf().disable() 30 | .authorizeRequests() 31 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 32 | 33 | .and() 34 | 35 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /video_code/1.simple_token/source/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.http.SessionCreationPolicy; 6 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 7 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 8 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; 9 | import org.springframework.security.oauth2.provider.token.RemoteTokenServices; 10 | 11 | @Configuration 12 | @EnableResourceServer 13 | public class SourceConfig extends ResourceServerConfigurerAdapter { 14 | 15 | @Override 16 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 17 | RemoteTokenServices tokenServices = new RemoteTokenServices(); 18 | 19 | tokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:3001/oauth/check_token"); 20 | 21 | tokenServices.setClientId("client1"); 22 | tokenServices.setClientSecret("123123"); 23 | 24 | resources.resourceId("resource1") 25 | .tokenServices(tokenServices) 26 | .stateless(true); 27 | } 28 | 29 | @Override 30 | public void configure(HttpSecurity http) throws Exception { 31 | http.csrf().disable() 32 | .authorizeRequests() 33 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 34 | 35 | .and() 36 | 37 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/src/main/java/lee/config/SourceConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.http.SessionCreationPolicy; 6 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 7 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 8 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; 9 | import org.springframework.security.oauth2.provider.token.RemoteTokenServices; 10 | 11 | @Configuration 12 | @EnableResourceServer 13 | public class SourceConfig extends ResourceServerConfigurerAdapter { 14 | 15 | @Override 16 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 17 | RemoteTokenServices tokenServices = new RemoteTokenServices(); 18 | 19 | tokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:3001/oauth/check_token"); 20 | 21 | tokenServices.setClientId("client1"); 22 | tokenServices.setClientSecret("123123"); 23 | 24 | resources.resourceId("resource1") 25 | .tokenServices(tokenServices) 26 | .stateless(true); 27 | } 28 | 29 | @Override 30 | public void configure(HttpSecurity http) throws Exception { 31 | http.csrf().disable() 32 | .authorizeRequests() 33 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 34 | 35 | .and() 36 | 37 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | public class MyUserDetails implements UserDetails { 12 | 13 | private String username; 14 | private String password; 15 | private String perms; 16 | 17 | @Override 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | @Override 23 | public boolean isAccountNonExpired() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean isAccountNonLocked() { 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean isCredentialsNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isEnabled() { 39 | return true; 40 | } 41 | 42 | public void setUsername(String username) { 43 | this.username = username; 44 | } 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 49 | } 50 | 51 | @Override 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPerms() { 61 | return perms; 62 | } 63 | 64 | public void setPerms(String perms) { 65 | this.perms = perms; 66 | } 67 | } -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | public class MyUserDetails implements UserDetails { 12 | 13 | private String username; 14 | private String password; 15 | private String perms; 16 | 17 | @Override 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | @Override 23 | public boolean isAccountNonExpired() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean isAccountNonLocked() { 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean isCredentialsNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isEnabled() { 39 | return true; 40 | } 41 | 42 | public void setUsername(String username) { 43 | this.username = username; 44 | } 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 49 | } 50 | 51 | @Override 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPerms() { 61 | return perms; 62 | } 63 | 64 | public void setPerms(String perms) { 65 | this.perms = perms; 66 | } 67 | } -------------------------------------------------------------------------------- /video_code/3.store_in_db/auth/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | public class MyUserDetails implements UserDetails { 12 | 13 | private String username; 14 | private String password; 15 | private String perms; 16 | 17 | @Override 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | @Override 23 | public boolean isAccountNonExpired() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean isAccountNonLocked() { 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean isCredentialsNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isEnabled() { 39 | return true; 40 | } 41 | 42 | public void setUsername(String username) { 43 | this.username = username; 44 | } 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 49 | } 50 | 51 | @Override 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPerms() { 61 | return perms; 62 | } 63 | 64 | public void setPerms(String perms) { 65 | this.perms = perms; 66 | } 67 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | public class MyUserDetails implements UserDetails { 12 | 13 | private String username; 14 | private String password; 15 | private String perms; 16 | 17 | @Override 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | @Override 23 | public boolean isAccountNonExpired() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean isAccountNonLocked() { 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean isCredentialsNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isEnabled() { 39 | return true; 40 | } 41 | 42 | public void setUsername(String username) { 43 | this.username = username; 44 | } 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 49 | } 50 | 51 | @Override 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPerms() { 61 | return perms; 62 | } 63 | 64 | public void setPerms(String perms) { 65 | this.perms = perms; 66 | } 67 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/auth/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | import java.util.stream.Collectors; 9 | import java.util.stream.Stream; 10 | 11 | public class MyUserDetails implements UserDetails { 12 | 13 | private String username; 14 | private String password; 15 | private String perms; 16 | 17 | @Override 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | @Override 23 | public boolean isAccountNonExpired() { 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean isAccountNonLocked() { 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean isCredentialsNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isEnabled() { 39 | return true; 40 | } 41 | 42 | public void setUsername(String username) { 43 | this.username = username; 44 | } 45 | 46 | @Override 47 | public Collection getAuthorities() { 48 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 49 | } 50 | 51 | @Override 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPerms() { 61 | return perms; 62 | } 63 | 64 | public void setPerms(String perms) { 65 | this.perms = perms; 66 | } 67 | } -------------------------------------------------------------------------------- /2_jwt_token/resource_server/src/main/java/lee/config/ResourceServerConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | 14 | //开启oauth2,reousrce server模式 15 | @EnableResourceServer 16 | public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 17 | 18 | @Autowired 19 | private TokenStore tokenStore; 20 | 21 | @Override 22 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 23 | resources 24 | //设置我这个resource的id, 这个在auth中配置, 这里必须照抄 25 | .resourceId("resource1") 26 | .tokenStore(tokenStore) 27 | 28 | //这个貌似是配置要不要把token信息记录在session中 29 | .stateless(true); 30 | } 31 | 32 | @Override 33 | public void configure(HttpSecurity http) throws Exception { 34 | http.csrf().disable() 35 | .authorizeRequests() 36 | 37 | //本项目所需要的授权范围,这个scope是写在auth服务的配置里的 38 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 39 | 40 | .and() 41 | 42 | //这个貌似是配置要不要把token信息记录在session中 43 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_1/src/main/java/lee/config/ResourceServerConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | 14 | //开启oauth2,reousrce server模式 15 | @EnableResourceServer 16 | public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 17 | 18 | @Autowired 19 | private TokenStore tokenStore; 20 | 21 | @Override 22 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 23 | resources 24 | //设置我这个resource的id, 这个在auth中配置, 这里必须照抄 25 | .resourceId("resource1") 26 | .tokenStore(tokenStore) 27 | 28 | //这个貌似是配置要不要把token信息记录在session中 29 | .stateless(true); 30 | } 31 | 32 | @Override 33 | public void configure(HttpSecurity http) throws Exception { 34 | http.csrf().disable() 35 | .authorizeRequests() 36 | 37 | //由于在zuul已经做了scope的校验,这里可以不写了.当然你想写上也是没有问题的 38 | .antMatchers("/**").permitAll()//.access("#oauth2.hasScope('scope1')") 39 | 40 | .and() 41 | 42 | //这个貌似是配置要不要把token信息记录在session中 43 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /4_spring_cloud/resource_server_2/src/main/java/lee/config/ResourceServerConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.http.SessionCreationPolicy; 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 | 12 | @Configuration 13 | 14 | //开启oauth2,reousrce server模式 15 | @EnableResourceServer 16 | public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 17 | 18 | @Autowired 19 | private TokenStore tokenStore; 20 | 21 | @Override 22 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 23 | resources 24 | //设置我这个resource的id, 这个在auth中配置, 这里必须照抄 25 | .resourceId("resource2") 26 | .tokenStore(tokenStore) 27 | 28 | //这个貌似是配置要不要把token信息记录在session中 29 | .stateless(true); 30 | } 31 | 32 | @Override 33 | public void configure(HttpSecurity http) throws Exception { 34 | http.csrf().disable() 35 | .authorizeRequests() 36 | 37 | //由于在zuul已经做了scope的校验,这里可以不写了.当然你想写上也是没有问题的 38 | .antMatchers("/**").permitAll()//.access("#oauth2.hasScope('scope1')") 39 | 40 | .and() 41 | 42 | //这个貌似是配置要不要把token信息记录在session中 43 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.2 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | net.sourceforge.htmlunit 27 | htmlunit 28 | 2.35.0 29 | 30 | 31 | 32 | com.alibaba 33 | fastjson 34 | 1.2.57 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /video_code/1.simple_token/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.2 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | net.sourceforge.htmlunit 27 | htmlunit 28 | 2.35.0 29 | 30 | 31 | 32 | com.alibaba 33 | fastjson 34 | 1.2.57 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.2 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | net.sourceforge.htmlunit 27 | htmlunit 28 | 2.35.0 29 | 30 | 31 | 32 | com.alibaba 33 | fastjson 34 | 1.2.57 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.2 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | net.sourceforge.htmlunit 27 | htmlunit 28 | 2.35.0 29 | 30 | 31 | 32 | com.alibaba 33 | fastjson 34 | 1.2.57 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.2 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | net.sourceforge.htmlunit 27 | htmlunit 28 | 2.35.0 29 | 30 | 31 | 32 | com.alibaba 33 | fastjson 34 | 1.2.57 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import lee.model.MyUserDetails; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 | import org.springframework.security.core.userdetails.UserDetailsService; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.crypto.password.PasswordEncoder; 13 | 14 | @Configuration 15 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Bean 19 | public PasswordEncoder passwordEncoder() { 20 | return new BCryptPasswordEncoder(); 21 | } 22 | 23 | //密码模式才需要配置,认证管理器 24 | @Bean 25 | @Override 26 | protected AuthenticationManager authenticationManager() throws Exception { 27 | return super.authenticationManager(); 28 | } 29 | 30 | @Override 31 | protected void configure(HttpSecurity http) throws Exception { 32 | http.csrf().disable() 33 | .authorizeRequests() 34 | .anyRequest().permitAll() 35 | 36 | .and() 37 | .formLogin() 38 | 39 | .and() 40 | .logout(); 41 | } 42 | 43 | @Bean 44 | public UserDetailsService userDetailsService() { 45 | return s -> { 46 | if ("admin".equals(s) || "user".equals(s)) { 47 | return new MyUserDetails(s, passwordEncoder().encode(s), s); 48 | } 49 | return null; 50 | }; 51 | } 52 | } -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import lee.model.MyUserDetails; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 | import org.springframework.security.core.userdetails.UserDetailsService; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.crypto.password.PasswordEncoder; 13 | 14 | @Configuration 15 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Bean 19 | public PasswordEncoder passwordEncoder() { 20 | return new BCryptPasswordEncoder(); 21 | } 22 | 23 | //密码模式才需要配置,认证管理器 24 | @Bean 25 | @Override 26 | protected AuthenticationManager authenticationManager() throws Exception { 27 | return super.authenticationManager(); 28 | } 29 | 30 | @Override 31 | protected void configure(HttpSecurity http) throws Exception { 32 | http.csrf().disable() 33 | .authorizeRequests() 34 | .anyRequest().permitAll() 35 | 36 | .and() 37 | .formLogin() 38 | 39 | .and() 40 | .logout(); 41 | } 42 | 43 | @Bean 44 | public UserDetailsService userDetailsService() { 45 | return s -> { 46 | if ("admin".equals(s) || "user".equals(s)) { 47 | return new MyUserDetails(s, passwordEncoder().encode(s), s); 48 | } 49 | return null; 50 | }; 51 | } 52 | } -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import lee.model.MyUserDetails; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 | import org.springframework.security.core.userdetails.UserDetailsService; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.crypto.password.PasswordEncoder; 13 | 14 | @Configuration 15 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Bean 19 | public PasswordEncoder passwordEncoder() { 20 | return new BCryptPasswordEncoder(); 21 | } 22 | 23 | //密码模式才需要配置,认证管理器 24 | @Bean 25 | @Override 26 | protected AuthenticationManager authenticationManager() throws Exception { 27 | return super.authenticationManager(); 28 | } 29 | 30 | @Override 31 | protected void configure(HttpSecurity http) throws Exception { 32 | http.csrf().disable() 33 | .authorizeRequests() 34 | .anyRequest().permitAll() 35 | 36 | .and() 37 | .formLogin() 38 | 39 | .and() 40 | .logout(); 41 | } 42 | 43 | @Bean 44 | public UserDetailsService userDetailsService() { 45 | return s -> { 46 | if ("admin".equals(s) || "user".equals(s)) { 47 | return new MyUserDetails(s, passwordEncoder().encode(s), s); 48 | } 49 | return null; 50 | }; 51 | } 52 | } -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/main/java/lee/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import lee.model.MyUserDetails; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10 | import org.springframework.security.core.userdetails.UserDetailsService; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.crypto.password.PasswordEncoder; 13 | 14 | @Configuration 15 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Bean 19 | public PasswordEncoder passwordEncoder() { 20 | return new BCryptPasswordEncoder(); 21 | } 22 | 23 | //密码模式才需要配置,认证管理器 24 | @Bean 25 | @Override 26 | protected AuthenticationManager authenticationManager() throws Exception { 27 | return super.authenticationManager(); 28 | } 29 | 30 | @Override 31 | protected void configure(HttpSecurity http) throws Exception { 32 | http.csrf().disable() 33 | .authorizeRequests() 34 | .anyRequest().permitAll() 35 | 36 | .and() 37 | .formLogin() 38 | 39 | .and() 40 | .logout(); 41 | } 42 | 43 | @Bean 44 | public UserDetailsService userDetailsService() { 45 | return s -> { 46 | if ("admin".equals(s) || "user".equals(s)) { 47 | return new MyUserDetails(s, passwordEncoder().encode(s), s); 48 | } 49 | return null; 50 | }; 51 | } 52 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/zuul/src/main/java/lee/filter/ScopeFilter.java: -------------------------------------------------------------------------------- 1 | package lee.filter; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.netflix.zuul.ZuulFilter; 5 | import com.netflix.zuul.context.RequestContext; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import java.io.UnsupportedEncodingException; 10 | import java.util.Base64; 11 | import java.util.List; 12 | 13 | @Component 14 | public class ScopeFilter extends ZuulFilter { 15 | @Override 16 | public String filterType() { 17 | return "pre"; 18 | } 19 | 20 | @Override 21 | public int filterOrder() { 22 | return 0; 23 | } 24 | 25 | @Override 26 | public boolean shouldFilter() { 27 | return true; 28 | } 29 | 30 | @Override 31 | public Object run() { 32 | 33 | RequestContext currentContext = RequestContext.getCurrentContext(); 34 | HttpServletRequest request = currentContext.getRequest(); 35 | String requestURI = request.getRequestURI(); 36 | 37 | if (requestURI.startsWith("/auth")) { 38 | return null; 39 | } 40 | 41 | String token = request.getHeader("Authorization"); 42 | 43 | token = token.split("\\.")[1]; 44 | 45 | byte[] bytes = Base64.getUrlDecoder().decode(token); 46 | try { 47 | token = new String(bytes, "UTF-8"); 48 | } catch (UnsupportedEncodingException e) { 49 | } 50 | 51 | JSONObject tokenJSON = JSONObject.parseObject(token); 52 | System.out.println(tokenJSON); 53 | 54 | List scope = tokenJSON.getJSONArray("scope").toJavaList(String.class); 55 | 56 | if (requestURI.startsWith("/source1") && scope.contains("scope1")) { 57 | return null; 58 | } 59 | 60 | if (requestURI.startsWith("/source2") && scope.contains("scope2")) { 61 | return null; 62 | } 63 | 64 | currentContext.setSendZuulResponse(false); 65 | currentContext.setResponseStatusCode(403); 66 | return null; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2_jwt_token/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /1_simple_token/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /3_store_in_db/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /4_spring_cloud/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /video_code/1.simple_token/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /video_code/3.store_in_db/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/test/src/test/java/lee/HttpUtil.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.*; 4 | import com.gargoylesoftware.htmlunit.util.NameValuePair; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.*; 9 | 10 | public class HttpUtil { 11 | 12 | public static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); 13 | 14 | public static Page send(HttpMethod httpMethod, String url, Map heads, String body, String... params) throws IOException { 15 | WebRequest webRequest = new WebRequest(new URL(url), httpMethod); 16 | 17 | if (httpMethod.equals(HttpMethod.GET)) { 18 | url += buildGetParam(params); 19 | } else { 20 | webRequest.setRequestParameters(buildPostParam(params)); 21 | } 22 | 23 | if (body != null) { 24 | webRequest.setRequestBody(body); 25 | } 26 | 27 | if (heads != null) { 28 | webRequest.setAdditionalHeaders(heads); 29 | } 30 | 31 | Page page = webClient.getPage(webRequest); 32 | System.out.println(page.getWebResponse().getContentAsString()); 33 | return page; 34 | } 35 | 36 | private static List buildPostParam(String... params) { 37 | List pairs = new ArrayList<>(); 38 | for (int i = 0; i < params.length; i += 2) { 39 | pairs.add(new NameValuePair(params[i], params[i + 1])); 40 | } 41 | return pairs; 42 | } 43 | 44 | private static String buildGetParam(String... params) { 45 | if (params == null || params.length == 0) { 46 | return ""; 47 | } 48 | String param = "?"; 49 | for (int i = 0; i < params.length; i += 2) { 50 | if (!param.equals("?")) { 51 | param += "&"; 52 | } 53 | String key = params[i]; 54 | String value = params[i + 1]; 55 | if (key.equals("id") && value == null) { 56 | continue; 57 | } 58 | param += key + "=" + value; 59 | } 60 | return param; 61 | } 62 | } -------------------------------------------------------------------------------- /1_simple_token/resource_server/src/main/java/lee/config/ResourceServerConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.http.SessionCreationPolicy; 6 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 7 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 8 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; 9 | import org.springframework.security.oauth2.provider.token.RemoteTokenServices; 10 | 11 | @Configuration 12 | 13 | //开启oauth2,reousrce server模式 14 | @EnableResourceServer 15 | public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 16 | 17 | @Override 18 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 19 | 20 | //远程token验证, 普通token必须远程校验 21 | RemoteTokenServices tokenServices = new RemoteTokenServices(); 22 | //配置去哪里验证token 23 | tokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:3001/oauth/check_token"); 24 | 25 | //配置组件的clientid和密码,这个也是在auth中配置好的 26 | tokenServices.setClientId("client1"); 27 | tokenServices.setClientSecret("123123"); 28 | 29 | resources 30 | //设置我这个resource的id, 这个在auth中配置, 这里必须照抄 31 | .resourceId("resource1") 32 | .tokenServices(tokenServices) 33 | 34 | //这个貌似是配置要不要把token信息记录在session中 35 | .stateless(true); 36 | } 37 | 38 | @Override 39 | public void configure(HttpSecurity http) throws Exception { 40 | http.csrf().disable() 41 | .authorizeRequests() 42 | 43 | //本项目所需要的授权范围,这个scope是写在auth服务的配置里的 44 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 45 | 46 | .and() 47 | 48 | //这个貌似是配置要不要把token信息记录在session中 49 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /3_store_in_db/resource_server/src/main/java/lee/config/ResourceServerConfig.java: -------------------------------------------------------------------------------- 1 | package lee.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.http.SessionCreationPolicy; 6 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 7 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 8 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; 9 | import org.springframework.security.oauth2.provider.token.RemoteTokenServices; 10 | 11 | @Configuration 12 | 13 | //开启oauth2,reousrce server模式 14 | @EnableResourceServer 15 | public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 16 | 17 | @Override 18 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 19 | 20 | //远程token验证, 普通token必须远程校验 21 | RemoteTokenServices tokenServices = new RemoteTokenServices(); 22 | //配置去哪里验证token 23 | tokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:3001/oauth/check_token"); 24 | 25 | //配置组件的clientid和密码,这个也是在auth中配置好的 26 | tokenServices.setClientId("client1"); 27 | tokenServices.setClientSecret("123123"); 28 | 29 | resources 30 | //设置我这个resource的id, 这个在auth中配置, 这里必须照抄 31 | .resourceId("resource1") 32 | .tokenServices(tokenServices) 33 | 34 | //这个貌似是配置要不要把token信息记录在session中 35 | .stateless(true); 36 | } 37 | 38 | @Override 39 | public void configure(HttpSecurity http) throws Exception { 40 | http.csrf().disable() 41 | .authorizeRequests() 42 | 43 | //本项目所需要的授权范围,这个scope是写在auth服务的配置里的 44 | .antMatchers("/**").access("#oauth2.hasScope('scope1')") 45 | 46 | .and() 47 | 48 | //这个貌似是配置要不要把token信息记录在session中 49 | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /1_simple_token/authorization_server/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | import java.util.stream.Stream; 13 | 14 | public class MyUserDetails implements UserDetails { 15 | 16 | private String username; 17 | private String password; 18 | private String perms; 19 | 20 | public MyUserDetails() { 21 | } 22 | 23 | public MyUserDetails(String username, String password, String perms) { 24 | this.username = username; 25 | this.password = password; 26 | this.perms = perms; 27 | } 28 | 29 | @Override 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | @Override 39 | public String getPassword() { 40 | return password; 41 | } 42 | 43 | public void setPassword(String password) { 44 | this.password = password; 45 | } 46 | 47 | public String getPerms() { 48 | return perms; 49 | } 50 | 51 | public void setPerms(String perms) { 52 | this.perms = perms; 53 | } 54 | 55 | //////////////////////////////////////////////// 56 | 57 | @Override 58 | public Collection getAuthorities() { 59 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 60 | } 61 | 62 | @Override 63 | public boolean isAccountNonExpired() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public boolean isAccountNonLocked() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean isCredentialsNonExpired() { 74 | return true; 75 | } 76 | 77 | @Override 78 | public boolean isEnabled() { 79 | return true; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | import java.util.stream.Stream; 13 | 14 | public class MyUserDetails implements UserDetails { 15 | 16 | private String username; 17 | private String password; 18 | private String perms; 19 | 20 | public MyUserDetails() { 21 | } 22 | 23 | public MyUserDetails(String username, String password, String perms) { 24 | this.username = username; 25 | this.password = password; 26 | this.perms = perms; 27 | } 28 | 29 | @Override 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | @Override 39 | public String getPassword() { 40 | return password; 41 | } 42 | 43 | public void setPassword(String password) { 44 | this.password = password; 45 | } 46 | 47 | public String getPerms() { 48 | return perms; 49 | } 50 | 51 | public void setPerms(String perms) { 52 | this.perms = perms; 53 | } 54 | 55 | //////////////////////////////////////////////// 56 | 57 | @Override 58 | public Collection getAuthorities() { 59 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 60 | } 61 | 62 | @Override 63 | public boolean isAccountNonExpired() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public boolean isAccountNonLocked() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean isCredentialsNonExpired() { 74 | return true; 75 | } 76 | 77 | @Override 78 | public boolean isEnabled() { 79 | return true; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /3_store_in_db/authorization_server/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | import java.util.stream.Stream; 13 | 14 | public class MyUserDetails implements UserDetails { 15 | 16 | private String username; 17 | private String password; 18 | private String perms; 19 | 20 | public MyUserDetails() { 21 | } 22 | 23 | public MyUserDetails(String username, String password, String perms) { 24 | this.username = username; 25 | this.password = password; 26 | this.perms = perms; 27 | } 28 | 29 | @Override 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | @Override 39 | public String getPassword() { 40 | return password; 41 | } 42 | 43 | public void setPassword(String password) { 44 | this.password = password; 45 | } 46 | 47 | public String getPerms() { 48 | return perms; 49 | } 50 | 51 | public void setPerms(String perms) { 52 | this.perms = perms; 53 | } 54 | 55 | //////////////////////////////////////////////// 56 | 57 | @Override 58 | public Collection getAuthorities() { 59 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 60 | } 61 | 62 | @Override 63 | public boolean isAccountNonExpired() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public boolean isAccountNonLocked() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean isCredentialsNonExpired() { 74 | return true; 75 | } 76 | 77 | @Override 78 | public boolean isEnabled() { 79 | return true; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /4_spring_cloud/authorization_server/src/main/java/lee/model/MyUserDetails.java: -------------------------------------------------------------------------------- 1 | package lee.model; 2 | 3 | import org.springframework.security.core.GrantedAuthority; 4 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | import java.util.stream.Stream; 13 | 14 | public class MyUserDetails implements UserDetails { 15 | 16 | private String username; 17 | private String password; 18 | private String perms; 19 | 20 | public MyUserDetails() { 21 | } 22 | 23 | public MyUserDetails(String username, String password, String perms) { 24 | this.username = username; 25 | this.password = password; 26 | this.perms = perms; 27 | } 28 | 29 | @Override 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | @Override 39 | public String getPassword() { 40 | return password; 41 | } 42 | 43 | public void setPassword(String password) { 44 | this.password = password; 45 | } 46 | 47 | public String getPerms() { 48 | return perms; 49 | } 50 | 51 | public void setPerms(String perms) { 52 | this.perms = perms; 53 | } 54 | 55 | //////////////////////////////////////////////// 56 | 57 | @Override 58 | public Collection getAuthorities() { 59 | return Stream.of(perms.split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); 60 | } 61 | 62 | @Override 63 | public boolean isAccountNonExpired() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public boolean isAccountNonLocked() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public boolean isCredentialsNonExpired() { 74 | return true; 75 | } 76 | 77 | @Override 78 | public boolean isEnabled() { 79 | return true; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /2_jwt_token/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | net.sourceforge.htmlunit 24 | htmlunit 25 | 2.35.0 26 | 27 | 28 | 29 | com.alibaba 30 | fastjson 31 | 1.2.57 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /1_simple_token/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | net.sourceforge.htmlunit 24 | htmlunit 25 | 2.35.0 26 | 27 | 28 | 29 | com.alibaba 30 | fastjson 31 | 1.2.57 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /3_store_in_db/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | net.sourceforge.htmlunit 24 | htmlunit 25 | 2.35.0 26 | 27 | 28 | 29 | com.alibaba 30 | fastjson 31 | 1.2.57 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /4_spring_cloud/test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | test 13 | 0.0.1-SNAPSHOT 14 | test 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | net.sourceforge.htmlunit 24 | htmlunit 25 | 2.35.0 26 | 27 | 28 | 29 | com.alibaba 30 | fastjson 31 | 1.2.57 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/auth/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | auth 13 | 0.0.1-SNAPSHOT 14 | auth 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/1.simple_token/auth/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | auth 13 | 0.0.1-SNAPSHOT 14 | auth 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/source/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/1.simple_token/source/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/3.store_in_db/source/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/4.jwt_store_in_db/source/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | ${spring-cloud.version} 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/eureka/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | eureka 13 | 0.0.1-SNAPSHOT 14 | eureka 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-netflix-eureka-server 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-dependencies 43 | ${spring-cloud.version} 44 | pom 45 | import 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /4_spring_cloud/eureka/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | eureka 13 | 0.0.1-SNAPSHOT 14 | eureka 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-server 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | org.junit.vintage 35 | junit-vintage-engine 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /video_code/2.jwt_token/test/src/test/java/lee/TT.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.HttpMethod; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.io.IOException; 7 | import java.util.Map; 8 | 9 | public class TT { 10 | 11 | public static String authorization_server = "http://127.0.0.1:3001/"; 12 | public static String resource_server = "http://127.0.0.1:4001/"; 13 | 14 | //密码模式 15 | @Test 16 | public void getTokenByPassword() throws IOException { 17 | //直接用用户的账号密码去申请权限,会把密码泄露给客户端 18 | String[] params = new String[]{ 19 | "client_id", "client1", 20 | "client_secret", "123123", 21 | "grant_type", "password", 22 | "username", "admin", 23 | "password", "admin" 24 | }; 25 | HttpUtil.send(HttpMethod.POST, authorization_server + "oauth/token", null, null, params); 26 | //{"access_token":"8d29e6ea-6b92-4a9d-8c73-fe36dbf8ff35","token_type":"bearer","refresh_token":"ac9d9203-9106-427b-a285-287a20fe4a42","expires_in":299,"scope":"all"} 27 | } 28 | 29 | //校验token 30 | @Test 31 | public void checkToken() throws IOException { 32 | String[] params = new String[]{ 33 | "token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UxIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUxIiwic2NvcGUyIl0sImV4cCI6MTYxMzk4Nzk5OCwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiNzM1MTBmNDUtODY4ZS00ZWQ2LTlmZmMtMzU5NzQ4OTBhMzQ5IiwiY2xpZW50X2lkIjoiY2xpZW50MSJ9._oexcj4pjEik-cvG0c2g9lGvuXgdkaaVTQvaZG1gg_4", 34 | }; 35 | HttpUtil.send(HttpMethod.POST, authorization_server + "oauth/check_token", null, null, params); 36 | //{"aud":["resource1"],"user_name":"admin","scope":["all"],"active":true,"exp":1582620698,"authorities":["admin"],"client_id":"client1"} 37 | } 38 | 39 | //使用token访问resource 40 | @Test 41 | public void getResourceByToken() throws IOException { 42 | Map head = RootUtil.buildMap("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UxIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUxIiwic2NvcGUyIl0sImV4cCI6MTYxMzg5NDU0MiwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiMDYzOTkwNWYtZGIxMi00NTA3LTgzNGQtYWZmYTIxYTU3MGVjIiwiY2xpZW50X2lkIjoiY2xpZW50MSJ9.x8FFZxne9_egr_99S--HILKrLquZ9Ao4y-x2TyC0vkA"); 43 | HttpUtil.send(HttpMethod.POST, resource_server + "/admin", head, null); 44 | } 45 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/auth/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | auth 13 | 0.0.1-SNAPSHOT 14 | auth 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/source2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | source 13 | 0.0.1-SNAPSHOT 14 | source 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | Hoxton.SR1 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-oauth2 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/test/java/lee/testClient1.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.HttpMethod; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.io.IOException; 7 | import java.util.Map; 8 | 9 | public class testClient1 { 10 | 11 | public static String zuul = "http://127.0.0.1:5001/"; 12 | 13 | //授权码模式 14 | @Test 15 | public void getTokenByCode() throws IOException { 16 | //标准模式 17 | //浏览器访问 18 | //http://127.0.0.1:5001/auth/oauth/authorize?client_id=client1&response_type=code&scope=scope1&redirect_uri=http://www.baidu.com 19 | 20 | //重定向结果 21 | //https://www.baidu.com/?code=r9zqyd 22 | 23 | String[] params = new String[]{ 24 | "client_id", "client1", 25 | "client_secret", "123123", 26 | "grant_type", "authorization_code", 27 | "code", "6FnG6s",//这个code是从getCode()方法获取的 28 | "redirect_uri", "http://www.baidu.com" 29 | }; 30 | HttpUtil.send(HttpMethod.POST, zuul + "auth/oauth/token", null, null, params); 31 | //{"access_token":"415b010c-4891-41ef-90ce-1653ecd37658","token_type":"bearer","refresh_token":"02021d0e-34ed-42eb-8abc-f495b2dcd512","expires_in":133,"scope":"all"} 32 | } 33 | 34 | //校验token 35 | @Test 36 | public void checkToken() throws IOException { 37 | String[] params = new String[]{ 38 | "token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UxIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUxIl0sImV4cCI6MTYxNDA5MjE1MiwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiNzM0ZWNiNGItZmMyOC00ZmNkLThkN2QtNjBhZTE3YWJhOGI0IiwiY2xpZW50X2lkIjoiY2xpZW50MSJ9.YsGNyTusTrYgqfd9GPTyVqq4Jw0foGobCvIeGKmTftI", 39 | }; 40 | HttpUtil.send(HttpMethod.POST, zuul + "auth/oauth/check_token", null, null, params); 41 | //{"aud":["resource1"],"user_name":"admin","scope":["all"],"active":true,"exp":1582620698,"authorities":["admin"],"client_id":"client1"} 42 | } 43 | 44 | //使用token访问resource 45 | @Test 46 | public void getResourceByToken() throws IOException { 47 | Map head = RootUtil.buildMap("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UxIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUxIl0sImV4cCI6MTYxNDA5MjE1MiwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiNzM0ZWNiNGItZmMyOC00ZmNkLThkN2QtNjBhZTE3YWJhOGI0IiwiY2xpZW50X2lkIjoiY2xpZW50MSJ9.YsGNyTusTrYgqfd9GPTyVqq4Jw0foGobCvIeGKmTftI"); 48 | HttpUtil.send(HttpMethod.POST, zuul + "source2/me", head, null); 49 | } 50 | } -------------------------------------------------------------------------------- /video_code/5.spring_cloud/test/src/test/java/lee/testClient2.java: -------------------------------------------------------------------------------- 1 | package lee; 2 | 3 | import com.gargoylesoftware.htmlunit.HttpMethod; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.io.IOException; 7 | import java.util.Map; 8 | 9 | public class testClient2 { 10 | 11 | public static String zuul = "http://127.0.0.1:5001/"; 12 | 13 | //授权码模式 14 | @Test 15 | public void getTokenByCode() throws IOException { 16 | //标准模式 17 | //浏览器访问 18 | //http://127.0.0.1:5001/auth/oauth/authorize?client_id=client2&response_type=code&scope=scope2&redirect_uri=http://www.sogou.com 19 | 20 | //重定向结果 21 | //https://www.baidu.com/?code=r9zqyd 22 | 23 | String[] params = new String[]{ 24 | "client_id", "client2", 25 | "client_secret", "123123", 26 | "grant_type", "authorization_code", 27 | "code", "Qw83wz",//这个code是从getCode()方法获取的 28 | "redirect_uri", "http://www.sogou.com" 29 | }; 30 | HttpUtil.send(HttpMethod.POST, zuul + "auth/oauth/token", null, null, params); 31 | //{"access_token":"415b010c-4891-41ef-90ce-1653ecd37658","token_type":"bearer","refresh_token":"02021d0e-34ed-42eb-8abc-f495b2dcd512","expires_in":133,"scope":"all"} 32 | } 33 | 34 | //校验token 35 | @Test 36 | public void checkToken() throws IOException { 37 | String[] params = new String[]{ 38 | "token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UyIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUyIl0sImV4cCI6MTYxNDA5MTAzNSwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiNTQwZWMxZDMtNGQ1OS00YjY3LWJjZTItYzM2NzI0MzRlNzk2IiwiY2xpZW50X2lkIjoiY2xpZW50MiJ9.LRtjPcaYykEjWAiYmZfLPeNUPKMddg8Q4jvhVxIKrzU", 39 | }; 40 | HttpUtil.send(HttpMethod.POST, zuul + "auth/oauth/check_token", null, null, params); 41 | //{"aud":["resource1"],"user_name":"admin","scope":["all"],"active":true,"exp":1582620698,"authorities":["admin"],"client_id":"client1"} 42 | } 43 | 44 | //使用token访问resource 45 | @Test 46 | public void getResourceByToken() throws IOException { 47 | Map head = RootUtil.buildMap("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2UyIl0sInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsic2NvcGUyIl0sImV4cCI6MTYxNDA5MTAzNSwiYXV0aG9yaXRpZXMiOlsiYWRtaW4iXSwianRpIjoiNTQwZWMxZDMtNGQ1OS00YjY3LWJjZTItYzM2NzI0MzRlNzk2IiwiY2xpZW50X2lkIjoiY2xpZW50MiJ9.LRtjPcaYykEjWAiYmZfLPeNUPKMddg8Q4jvhVxIKrzU"); 48 | HttpUtil.send(HttpMethod.POST, zuul + "source2/me", head, null); 49 | } 50 | } -------------------------------------------------------------------------------- /2_jwt_token/resource_server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | resource_server 13 | 0.0.1-SNAPSHOT 14 | resource_server 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-oauth2 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring-cloud.version} 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /1_simple_token/resource_server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | resource_server 13 | 0.0.1-SNAPSHOT 14 | resource_server 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-oauth2 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring-cloud.version} 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /3_store_in_db/resource_server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | resource_server 13 | 0.0.1-SNAPSHOT 14 | resource_server 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-oauth2 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring-cloud.version} 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /2_jwt_token/authorization_server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | lee 12 | authorization_server 13 | 0.0.1-SNAPSHOT 14 | authorization_server 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-oauth2 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring-cloud.version} 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | --------------------------------------------------------------------------------