springCloud oauth demo

pull/254/head
xjs 3 years ago
parent d96475b965
commit 0b6a41fba6

@ -32,6 +32,11 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -17,3 +17,9 @@ eureka:
instance: instance:
prefer-ip-address: true #服务实例中显示ip而不是显示主机名 prefer-ip-address: true #服务实例中显示ip而不是显示主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称 instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称
logging:
level:
org.springframework.cloud.sleuth: debug
org.springframework.web.servlet.DispatcherServlet: debug

@ -27,6 +27,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -21,3 +21,9 @@ eureka:
#自定义元数据 #自定义元数据
metadata-map: metadata-map:
now: 2022 now: 2022
logging:
level:
org.springframework.cloud.sleuth: debug
org.springframework.web.servlet.DispatcherServlet: debug

@ -0,0 +1,45 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-project</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>oauth2认证授权</name>
<artifactId>oauth2</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,18 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author xiejs
* @since 2022-05-30
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OAuthServerApplication9898 {
public static void main(String[] args) {
SpringApplication.run(OAuthServerApplication9898.class, args);
}
}

@ -0,0 +1,121 @@
package com.xjs.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
/**
*
*
* @author xiejs
* @since 2022-05-30
*/
@Configuration
@EnableAuthorizationServer
public class OauthServerConfiger extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
/**
* api<br>
* api访
*
* @param security
* @throws Exception
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
super.configure(security);
//相当于打开endpoints访问接口的开关这样的话后期我们能够访问该接口
security
//允许客户端表单认证
.allowFormAuthenticationForClients()
//开启端口 oauth/token_key的访问权限
.tokenKeyAccess("permitAll()")
//开启端口 oauth/check_token的访问权限
.checkTokenAccess("permitAll()")
;
}
/**
* <br>
* client_id,secret<br>
* QQQQQQQQclient_id
*
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
super.configure(clients);
clients.inMemory() //客户端信息存储到什么地方,可以再内存中,也可以再数据库里
.withClient("client_A") //添加一份client配置指定client_id
.secret("xjs666") //指定客户端的密码/安全码
.resourceIds("autodeliver") //指定客户端所能访问的资源id清单此处的资源id是需要在具体的资源服务
.authorizedGrantTypes("password", "refresh_token") //认证类型/令牌颁发模式,可以配置多个,需要客户端调用的时候传递参数
.scopes("all") //客户端权限范围
;
}
/**
* token
*
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
super.configure(endpoints);
endpoints.tokenStore(this.tokenStore()) //指定token的存储方法
.tokenServices(this.authorizationServerTokenServices()) //token服务的一个描述可以认为是token生成细节的描述
.authenticationManager(authenticationManager) //指定认证管理器,随后注入一个到当前类使用即可
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
;
}
/**
* tokenStore<br>
* token
*/
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
/**
* tokentoken
*/
public AuthorizationServerTokenServices authorizationServerTokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
//是否开启令牌刷新
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setTokenStore(this.tokenStore());
//设置令牌有效时间
defaultTokenServices.setAccessTokenValiditySeconds(10 /** 60*/);
//设置刷新令牌的有效时间
defaultTokenServices.setRefreshTokenValiditySeconds(3 * 60 * 60 * 24);
return defaultTokenServices;
}
}

@ -0,0 +1,66 @@
package com.xjs.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.ArrayList;
/**
*
*
* @author xiejs
* @since 2022-05-30
*/
@Configuration
public class SecurityConfiger extends WebSecurityConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
/**
*
*/
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
*
* <li>
* <ul>usernamepassword</ul>
* <ul>usernamepassword</ul>
* <ul></ul>
* </li>
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
//把用户信息配置在内存中,实例化一个用户对象
UserDetails user = new User("admin", "123456", new ArrayList<>());
auth.inMemoryAuthentication()
.withUser(user)
.passwordEncoder(passwordEncoder)
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}

@ -0,0 +1,18 @@
server:
port: 9898
spring:
application:
name: oauth-server
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群就把多个Eureka Server 地址使用逗号连接起来即可,注册到单实例,就写一个
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
instance:
prefer-ip-address: true #服务实例中显示ip而不是显示主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称

@ -13,6 +13,7 @@
<modules> <modules>
<module>eureka</module> <module>eureka</module>
<module>stream</module> <module>stream</module>
<module>oauth2</module>
</modules> </modules>
<artifactId>springcloud-project</artifactId> <artifactId>springcloud-project</artifactId>

@ -28,8 +28,8 @@ eureka:
client: client:
serviceUrl: # eureka server的路径 serviceUrl: # eureka server的路径
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
instance: instance:
prefer-ip-address: true #使用ip注册 prefer-ip-address: true #使用ip注册
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称 instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称

Loading…
Cancel
Save