提交 e1c328a7 编写于 作者: 起风了_ZXJ's avatar 起风了_ZXJ

初始化项目

上级 ac77ffdf
/*
Navicat Premium Data Transfer
Source Server : localhost_mysql
Source Server Type : MySQL
Source Server Version : 50724
Source Host : localhost:3306
Source Schema : oauth2
Target Server Type : MySQL
Target Server Version : 50724
File Encoding : 65001
Date: 01/01/2022 21:56:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for oauth_client_details
-- ----------------------------
DROP TABLE IF EXISTS `oauth_client_details`;
CREATE TABLE `oauth_client_details` (
`id` int(11) NOT NULL,
`client_id` varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`resource_ids` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`client_secret` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`scope` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`authorized_grant_types` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`web_server_redirect_uri` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`authorities` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`access_token_validity` int(11) NULL DEFAULT NULL,
`refresh_token_validity` int(11) NULL DEFAULT NULL,
`additional_information` varchar(4096) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`autoapprove` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of oauth_client_details
-- ----------------------------
INSERT INTO `oauth_client_details` VALUES (1, 'client_2', NULL, '$2a$10$qe50PyR/XoF25SCewslYP.Bi0wyEM7sJHGdwgEjbEMsSC6ukhMtpm', 'select', 'password', NULL, NULL, NULL, NULL, NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1;
#user信息在UserDetailsServiceImpl验证,client信息在数据库表oauth_client_details验证
http://localhost/oauth/token?username=user&password=123456&grant_type=password&scope=select&client_id=client_2&client_secret=123456
#测试资源(token为前面获取到的)
http://localhost/a?access_token=cd43d91f-c222-4db1-84df-a2dcb312f029
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-security:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.2" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.9" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.32" level="project" />
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-config:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-web:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
<orderEntry type="library" name="Maven: io.lettuce:lettuce-core:6.1.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-tcnative-classes:2.0.46.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.4.13" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-actuator:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-actuator-autoconfigure:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-actuator:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.1" level="project" />
<orderEntry type="library" name="Maven: io.micrometer:micrometer-core:1.8.1" level="project" />
<orderEntry type="library" name="Maven: org.hdrhistogram:HdrHistogram:2.1.12" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.latencyutils:LatencyUtils:2.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security.oauth:spring-security-oauth2:2.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-crypto:5.6.1" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.4" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.1" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.27" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-security:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.2" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.9" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.32" level="project" />
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-config:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-web:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
<orderEntry type="library" name="Maven: io.lettuce:lettuce-core:6.1.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-tcnative-classes:2.0.46.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.72.Final" level="project" />
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.4.13" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-actuator:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-actuator-autoconfigure:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-actuator:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.1" level="project" />
<orderEntry type="library" name="Maven: io.micrometer:micrometer-core:1.8.1" level="project" />
<orderEntry type="library" name="Maven: org.hdrhistogram:HdrHistogram:2.1.12" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.latencyutils:LatencyUtils:2.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security.oauth:spring-security-oauth2:2.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.6.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-crypto:5.6.1" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.56" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.6.2" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.14" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.4" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.1" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.27" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
</component>
</module>
\ No newline at end of file
<?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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>oauth-demo-new</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>oauth2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.oauth2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Oauth2Application {
public static void main(String[] args) {
SpringApplication.run(Oauth2Application.class, args);
}
}
package com.example.oauth2.test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.builders.JdbcClientDetailsServiceBuilder;
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.ClientDetailsService;
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.redis.RedisTokenStore;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Resource
private AuthenticationManager authenticationManager;
@Resource
private RedisConnectionFactory redisConnectionFactory;
@Resource
private UserDetailsService userDetailsService;
@Resource
private DataSource dataSource;
@Resource
private PasswordEncoder passwordEncoder;
@Resource
private ClientDetailsService clientDetailsService;
@Resource
private TokenStore tokenStore;
/**
* 指定token存储方式
*
* @return
*/
@Bean
public TokenStore tokenStore() {
//存储redis中
return new RedisTokenStore(redisConnectionFactory);
}
/**
* 配置token相关信息
* @return
*/
@Bean
public AuthorizationServerTokenServices tokenService() {
DefaultTokenServices service = new DefaultTokenServices();
service.setSupportRefreshToken(true);
service.setClientDetailsService(clientDetailsService);
service.setTokenStore(tokenStore);
service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
//service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
return service;
}
/**
* 配置令牌端点的安全约束
* @param security
* @throws Exception
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
//tokenkey这个endpoint当使用JwtToken且使用非对称加密时,资源服务用于获取公钥而开放的,这里指这个 endpoint完全公开
.tokenKeyAccess("permitAll()")
//checkToken这个endpoint完全公开
.checkTokenAccess("permitAll()")
// 允许表单认证
.allowFormAuthenticationForClients();
}
/**
* 用来配置客户端详情服务(ClientDetailsService),客户端详情信息在 这里进行初始化,你能够把客户端详情信息写死在这里或者是通过数据库来存储调取详情信息。
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//默认表名:oauth_client_details,结构见:doc文件夹下sql
JdbcClientDetailsServiceBuilder jcsb = clients.jdbc(dataSource);
jcsb.passwordEncoder(passwordEncoder);
}
/**
* 认证服务端点配置,用来配置令牌(token)的访问端点和令牌服务(token services)。
* @param endpoints
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
//用户管理
.userDetailsService(userDetailsService)
//token存到redis
.tokenStore(tokenStore)
//启用oauth2管理
.authenticationManager(authenticationManager)
//加载token配置
.tokenServices(tokenService())
//接收GET和POST
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
}
package com.example.oauth2.test2;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
/**
* @Classname MyResourceServerConfigurer
* @Description TODO
* @Date 2022-01-01 19:59
* @Created by Administrator
*/
//资源服务器用来验证token
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("abc").stateless(true);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
\ No newline at end of file
package com.example.oauth2.test2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
Logger logger = LoggerFactory.getLogger(TestController.class);
@GetMapping("/a")
public String testSwagger1() {
logger.info("我是日志 aaaaa");
System.out.println("aaaaaa");
return "aaaaa";
}
@GetMapping("/b")
public String testSwagger2() {
logger.info("我是日志 bbbbbb");
System.out.println("bbbbbbbb");
return "bbbbbb";
}
}
package com.example.oauth2.test2;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
//@Autowired
//private UserServiceImpl userService;
@Resource
private RedisConnectionFactory redisConnectionFactory;
/**
* 实现UserDetailsService中的loadUserByUsername方法,用于加载用户数据
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
OAuth2AccessToken oAuth2AccessToken = new RedisTokenStore(redisConnectionFactory).readAccessToken("7a99cc45-42ce-4447-acbe-f30756dde928");
System.out.println(oAuth2AccessToken);
//登录账号
logger.info("当前登录用户:username:{} 登录时间:{}", username, new Date());
// 根据账号去数据库查询...
/* UserDto user = userDao.getUserByUsername(username);
if (user == null) {
return null;
}*/
//这里暂时使用静态数据
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("select"));
//模拟查询是否存在
if (!"user".equals(username)) {
throw new UsernameNotFoundException(username);
}
//密码是加密的
String password = new BCryptPasswordEncoder().encode("123456");
UserDetails userDetails = User.withUsername(username).password(password).authorities(authorities).build();
return userDetails;
}
}
package com.example.oauth2.test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* BCryptPasswordEncoder进行密码加密
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* 认证管理器
* @return
* @throws Exception
*/
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.anyRequest()
//设置符合条件的端点通过,不被保护
.and()
.authorizeRequests()
.antMatchers("/oauth/*")
.permitAll();
}
}
spring:
application:
name: oauth2-server
redis:
database: 2
host: localhost
password:
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
timeout: 100ms
main:
allow-bean-definition-overriding: true
allow-circular-references: true
datasource:
url: jdbc:mysql://127.0.0.1:3306/oauth?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
username: root
password: root
hikari:
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
maximum-pool-size: 9
server:
port: 80
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册