基于@EnableOAuth2Sso实现单点登录
This commit is contained in:
parent
c5f9146bac
commit
c4256d46f9
@ -39,7 +39,7 @@ public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
|
|||||||
.withClient("web") //客户端名称,随便起就行
|
.withClient("web") //客户端名称,随便起就行
|
||||||
.secret(encoder.encode("654321")) //只与客户端分享的secret,随便写,但是注意要加密
|
.secret(encoder.encode("654321")) //只与客户端分享的secret,随便写,但是注意要加密
|
||||||
.autoApprove(false) //自动审批,这里关闭,要的就是一会体验那种感觉
|
.autoApprove(false) //自动审批,这里关闭,要的就是一会体验那种感觉
|
||||||
.redirectUris("http://localhost:8201/login") //可以写多个,当有多个时需要在验证请求中指定使用哪个地址进行回调
|
.redirectUris("http://localhost:8101/login", "http://localhost:8201/login", "http://localhost:8301/login") //可以写多个,当有多个时需要在验证请求中指定使用哪个地址进行回调
|
||||||
.scopes("book", "user", "borrow") //授权范围,这里我们使用全部all
|
.scopes("book", "user", "borrow") //授权范围,这里我们使用全部all
|
||||||
.authorizedGrantTypes("client_credentials", "password", "implicit", "authorization_code", "refresh_token");
|
.authorizedGrantTypes("client_credentials", "password", "implicit", "authorization_code", "refresh_token");
|
||||||
//授权模式,一共支持5种,除了之前我们介绍的四种之外,还有一个刷新Token的模式
|
//授权模式,一共支持5种,除了之前我们介绍的四种之外,还有一个刷新Token的模式
|
||||||
|
@ -19,6 +19,16 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||||
|
<version>2.2.5.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package com.test;
|
package com.test;
|
||||||
|
|
||||||
import com.apple.eawt.Application;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: BookApplication
|
* ClassName: BookApplication
|
||||||
@ -11,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
* @author yovinchen
|
* @author yovinchen
|
||||||
* @Create 2023/8/14 16:23
|
* @Create 2023/8/14 16:23
|
||||||
*/
|
*/
|
||||||
|
@EnableOAuth2Sso
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class BookApplication {
|
public class BookApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.test.controller;
|
package com.test.controller;
|
||||||
|
|
||||||
import com.test.service.BookService;
|
import com.test.service.BookService;
|
||||||
|
import org.springframework.security.core.context.SecurityContext;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -23,6 +25,9 @@ public class BookController {
|
|||||||
|
|
||||||
@RequestMapping("/book/{bid}")
|
@RequestMapping("/book/{bid}")
|
||||||
Book findBookById(@PathVariable("bid") int bid){
|
Book findBookById(@PathVariable("bid") int bid){
|
||||||
|
//通过SecurityContextHolder将用户信息取出
|
||||||
|
SecurityContext context = SecurityContextHolder.getContext();
|
||||||
|
System.out.println(context.getAuthentication());
|
||||||
return service.getBookById(bid);
|
return service.getBookById(bid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,3 +6,16 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
#不多说了
|
||||||
|
client-id: web
|
||||||
|
client-secret: 654321
|
||||||
|
#Token获取地址
|
||||||
|
access-token-uri: http://localhost:8500/sso/oauth/token
|
||||||
|
#验证页面地址
|
||||||
|
user-authorization-uri: http://localhost:8500/sso/oauth/authorize
|
||||||
|
resource:
|
||||||
|
#Token信息获取和校验地址
|
||||||
|
token-info-uri: http://localhost:8500/sso/oauth/check_token
|
||||||
|
@ -18,6 +18,16 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||||
|
<version>2.2.5.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -2,6 +2,7 @@ package com.test;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: BorrowApplication
|
* ClassName: BorrowApplication
|
||||||
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
* @author yovinchen
|
* @author yovinchen
|
||||||
* @Create 2023/8/14 16:25
|
* @Create 2023/8/14 16:25
|
||||||
*/
|
*/
|
||||||
|
@EnableOAuth2Sso
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class BorrowApplication {
|
public class BorrowApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -6,3 +6,16 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
#不多说了
|
||||||
|
client-id: web
|
||||||
|
client-secret: 654321
|
||||||
|
#Token获取地址
|
||||||
|
access-token-uri: http://localhost:8500/sso/oauth/token
|
||||||
|
#验证页面地址
|
||||||
|
user-authorization-uri: http://localhost:8500/sso/oauth/authorize
|
||||||
|
resource:
|
||||||
|
#Token信息获取和校验地址
|
||||||
|
token-info-uri: http://localhost:8500/sso/oauth/check_token
|
||||||
|
10
pom.xml
10
pom.xml
@ -25,6 +25,16 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||||
|
<version>2.2.5.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
@ -2,6 +2,7 @@ package com.test;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: UserApplication
|
* ClassName: UserApplication
|
||||||
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
* @author yovinchen
|
* @author yovinchen
|
||||||
* @Create 2023/8/14 16:26
|
* @Create 2023/8/14 16:26
|
||||||
*/
|
*/
|
||||||
|
@EnableOAuth2Sso
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class UserApplication {
|
public class UserApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -6,3 +6,16 @@ spring:
|
|||||||
url: jdbc:mysql://43.143.164.194:3306/mac
|
url: jdbc:mysql://43.143.164.194:3306/mac
|
||||||
username: mac
|
username: mac
|
||||||
password: mactest
|
password: mactest
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
#不多说了
|
||||||
|
client-id: web
|
||||||
|
client-secret: 654321
|
||||||
|
#Token获取地址
|
||||||
|
access-token-uri: http://localhost:8500/sso/oauth/token
|
||||||
|
#验证页面地址
|
||||||
|
user-authorization-uri: http://localhost:8500/sso/oauth/authorize
|
||||||
|
resource:
|
||||||
|
#Token信息获取和校验地址
|
||||||
|
token-info-uri: http://localhost:8500/sso/oauth/check_token
|
||||||
|
Loading…
Reference in New Issue
Block a user