Skip to content

Commit 1fc0694

Browse files
committed
add oauth2-client
1 parent b749e5f commit 1fc0694

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed

oauth2-client/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

oauth2-client/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.7.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.macro.cloud</groupId>
12+
<artifactId>oauth2-client</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>oauth2-client</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.cloud</groupId>
25+
<artifactId>spring-cloud-starter-oauth2</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-security</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-web</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>io.jsonwebtoken</groupId>
37+
<artifactId>jjwt</artifactId>
38+
<version>0.9.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>cn.hutool</groupId>
42+
<artifactId>hutool-all</artifactId>
43+
<version>4.6.3</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<dependencyManagement>
54+
<dependencies>
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-dependencies</artifactId>
58+
<version>${spring-cloud.version}</version>
59+
<type>pom</type>
60+
<scope>import</scope>
61+
</dependency>
62+
</dependencies>
63+
</dependencyManagement>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.macro.cloud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
6+
7+
@EnableOAuth2Sso
8+
@SpringBootApplication
9+
public class Oauth2ClientApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Oauth2ClientApplication.class, args);
13+
}
14+
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.macro.cloud.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
8+
/**
9+
* 在接口上配置权限时使用
10+
* Created by macro on 2019/10/11.
11+
*/
12+
@Configuration
13+
@EnableGlobalMethodSecurity(prePostEnabled = true)
14+
@Order(101)
15+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.macro.cloud.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.security.core.Authentication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
/**
9+
* Created by macro on 2019/10/11.
10+
*/
11+
@RestController
12+
public class IndexController {
13+
@Autowired
14+
private UserController userController;
15+
16+
@GetMapping("/")
17+
public Object index(Authentication authentication) {
18+
return userController.getCurrentUser(authentication);
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.macro.cloud.controller;
2+
3+
import org.springframework.security.access.prepost.PreAuthorize;
4+
import org.springframework.security.core.Authentication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* Created by macro on 2019/9/30.
11+
*/
12+
@RestController
13+
@RequestMapping("/user")
14+
public class UserController {
15+
@GetMapping("/getCurrentUser")
16+
public Object getCurrentUser(Authentication authentication) {
17+
return authentication;
18+
}
19+
20+
@PreAuthorize("hasAuthority('admin')")
21+
@GetMapping("/auth/admin")
22+
public Object adminAuth() {
23+
return "Has admin auth!";
24+
}
25+
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server:
2+
port: 9501
3+
servlet:
4+
session:
5+
cookie:
6+
name: OAUTH2-CLIENT-SESSIONID #防止Cookie冲突,冲突会导致登录验证不通过
7+
oauth2-server-url: http://localhost:9401
8+
spring:
9+
application:
10+
name: oauth2-client
11+
security:
12+
oauth2: #与oauth2-server对应的配置
13+
client:
14+
client-id: admin
15+
client-secret: admin123456
16+
user-authorization-uri: ${oauth2-server-url}/oauth/authorize
17+
access-token-uri: ${oauth2-server-url}/oauth/token
18+
resource:
19+
jwt:
20+
key-uri: ${oauth2-server-url}/oauth/token_key
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.macro.cloud;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class Oauth2ClientApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)