基于@EnableOAuth2Sso实现单点登录
This commit is contained in:
		@@ -19,6 +19,16 @@
 | 
			
		||||
    </properties>
 | 
			
		||||
 | 
			
		||||
    <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>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-starter-web</artifactId>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
package com.test;
 | 
			
		||||
 | 
			
		||||
import com.apple.eawt.Application;
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: BookApplication
 | 
			
		||||
@@ -11,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/8/14 16:23
 | 
			
		||||
 */
 | 
			
		||||
@EnableOAuth2Sso
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
public class BookApplication {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
package com.test.controller;
 | 
			
		||||
 | 
			
		||||
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.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
@@ -23,6 +25,9 @@ public class BookController {
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/book/{bid}")
 | 
			
		||||
    Book findBookById(@PathVariable("bid") int bid){
 | 
			
		||||
        //通过SecurityContextHolder将用户信息取出
 | 
			
		||||
        SecurityContext context = SecurityContextHolder.getContext();
 | 
			
		||||
        System.out.println(context.getAuthentication());
 | 
			
		||||
        return service.getBookById(bid);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,3 +6,16 @@ spring:
 | 
			
		||||
    url: jdbc:mysql://43.143.164.194:3306/mac
 | 
			
		||||
    username: mac
 | 
			
		||||
    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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user