整合基础框架

This commit is contained in:
YoVinchen 2023-08-19 20:21:30 +08:00
parent 5c8439cb7f
commit 5e0259bc2a
6 changed files with 172 additions and 0 deletions

39
auth-service/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>SpringCloudStudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>auth-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- OAuth2.0依赖,不再内置了,所以得我们自己指定一下版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,18 @@
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* ClassName: AuthApplication
* Package: com.test
*
* @author yovinchen
* @Create 2023/8/19 20:14
*/
@SpringBootApplication
public class AuthApplication {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class,args);
}
}

View File

@ -0,0 +1,60 @@
package com.test.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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 javax.annotation.Resource;
/**
* ClassName: OAuth2Configuration
* Package: com.test.config
*
* @author yovinchen
* @Create 2023/8/19 20:17
*/
@EnableAuthorizationServer //开启验证服务器
@Configuration
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
private final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
@Resource
private AuthenticationManager manager;
/**
* 这个方法是对客户端进行配置一个验证服务器可以预设很多个客户端
* 之后这些指定的客户端就可以按照下面指定的方式进行验证
*
* @param clients 客户端配置工具
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory() //这里我们直接硬编码创建当然也可以像Security那样自定义或是使用JDBC从数据库读取
.withClient("web") //客户端名称随便起就行
.secret(encoder.encode("654321")) //只与客户端分享的secret随便写但是注意要加密
.autoApprove(false) //自动审批这里关闭要的就是一会体验那种感觉
.scopes("book", "user", "borrow") //授权范围这里我们使用全部all
.authorizedGrantTypes("client_credentials", "password", "implicit", "authorization_code", "refresh_token");
//授权模式一共支持5种除了之前我们介绍的四种之外还有一个刷新Token的模式
//这里我们直接把五种都写上方便一会实验当然各位也可以单独只写一种一个一个进行测试
//现在我们指定的客户端就支持这五种类型的授权方式了
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
security.passwordEncoder(encoder) //编码器设定为BCryptPasswordEncoder
.allowFormAuthenticationForClients() //允许客户端使用表单验证一会我们POST请求中会携带表单信息
.checkTokenAccess("permitAll()"); //允许所有的Token查询请求
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.authenticationManager(manager);
//由于SpringSecurity新版本的一些底层改动这里需要配置一下authenticationManager才能正常使用password模式
}
}

View File

@ -0,0 +1,40 @@
package com.test.config;
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.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* ClassName: SecurityConfiguration
* Package: com.test.config
*
* @author yovinchen
* @Create 2023/8/19 20:14
*/
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated() //
.and().formLogin().permitAll(); //使用表单登录
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication() //直接创建一个用户懒得搞数据库了
.passwordEncoder(encoder).withUser("test").password(encoder.encode("123456")).roles("USER");
}
@Bean //这里需要将AuthenticationManager注册为Bean在OAuth配置中使用
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}

View File

@ -0,0 +1,7 @@
server:
port: 8500
servlet:
#为了防止一会在服务之间跳转导致Cookie打架因为所有服务地址都是localhost都会存JSESSIONID
#这里修改一下context-path这样保存的Cookie会使用指定的路径就不会和其他服务打架了
#但是注意之后的请求都得在最前面加上这个路径
context-path: /sso

View File

@ -19,6 +19,7 @@
<module>borrow-service</module> <module>borrow-service</module>
<module>book-service</module> <module>book-service</module>
<module>commons</module> <module>commons</module>
<module>auth-service</module>
</modules> </modules>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
@ -53,6 +54,13 @@
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version> <version>2.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build> <build>