(9)spring-cloud-starter-oauth2-下

目录

1、授权服务器

2、pom.xml

3、启动类

4、application.properties

5、Controller 

6、测试


本章主要介绍客户端怎么调用:

1、授权服务器

@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter
{
@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.inMemory()
				//配置client-id
				.withClient("admin")
				//配置client-secret
				.secret(passwordEncoder.encode("112233"))
				//配置访问token的有效期
				.accessTokenValiditySeconds(3600)
				//配置刷新Token的有效期
				.refreshTokenValiditySeconds(864000)
				//配置redirect_uri,用于授权成功后跳转
				.redirectUris("http://localhost:8081/login")
				//自动授权配置
				.autoApprove(true)
				//配置申请的权限范围
				.scopes("all")
				//配置grant_type,表示授权类型
				.authorizedGrantTypes("password","refresh_token","authorization_code");
	}
	
@Override
	public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
		//获取秘钥需要身份认证,使用单点登录时必须配置
		security.tokenKeyAccess("isAuthenticated()");
	}

}

为什么.redirectUris("http://localhost:8081/login") ?

因为http://localhost:8081中 spring-cloud-starter-security默认集成的登录就是/login

注意点:

 

2、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<groupId>com.xxxx</groupId>
	<artifactId>oauth2client01-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>oauth2client01-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-oauth2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>io.jsonwebtoken</groupId>
			<artifactId>jjwt</artifactId>
			<version>0.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3、启动类

@SpringBootApplication
//开启单点登录功能
@EnableOAuth2Sso
public class Oauth2client01DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(Oauth2client01DemoApplication.class, args);
	}

}

4、application.properties

server.port=8081
#防止Cookie冲突,冲突会导致登录验证不通过
server.servlet.session.cookie.name=OAUTH2-CLIENT-SESSIONID01
#授权服务器地址
oauth2-server-url: http://localhost:8080
#与授权服务器对应的配置
security.oauth2.client.client-id=client01
security.oauth2.client.client-secret=secret01
security.oauth2.client.user-authorization-uri=${oauth2-server-url}/oauth/authorize
security.oauth2.client.access-token-uri=${oauth2-server-url}/oauth/token
security.oauth2.resource.jwt.key-uri=${oauth2-server-url}/oauth/token_key
security.oauth2.resource.jwt.key-value=test_key

 security.oauth2.resource.jwt.key-value=test_key 对应授权服务器:

5、Controller 

@RestController
@RequestMapping("/user")
public class UserController {

	/**
	 * 获取当前用户信息
	 * @param authentication
	 * @return
	 */
	@RequestMapping("getCurrentUser")
	public Object getCurrentUser(Authentication authentication){
		return authentication;
	}

}

6、测试

http://localhost:8081/user/getCurrentUser
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值