Spring-Security-OAuth2是对OAuth2的一种实现,并且跟我们之前学习的Spring Security相辅相成。使用它来实现我们设计的分布式认证授权解决方案。
OAuth2.0的服务提供方涵盖两个服务,即授权服务 (Authorization Server,也叫认证服务) 和资源服务 (Resource Server)
授权服务 (Authorization Server)应包含对接入端以及登入用户的合法性进行验证并颁发token等功能,对令牌 的请求端点由 Spring MVC 控制器进行实现,
下面是配置一个认证服务必须要实现的endpoints:
AuthorizationEndpoint 服务于认证请求。默认 URL: /oauth/authorize 。
TokenEndpoint 服务于访问令牌的请求。默认 URL: /oauth/token 。
资源服务 (Resource Server):应包含对资源的保护功能,对非法请求进行拦截,对请求中token进行解析鉴权等:
OAuth2AuthenticationProcessingFilter 用来对请求给出的身份令牌解析鉴权。
分别创建uaa授权服务(也可叫认证服务)和order订单资源服务。
认证流程如下:
1、客户端请求UAA授权服务进行认证。
2、认证通过后由UAA颁发令牌。
3、客户端携带令牌Token请求资源服务。
4、资源服务校验令牌的合法性,合法即返回资源信息。
创建父工程,然后配置pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
spt-ds-uaa-server
spt-ds-order-server
com.ljf.springsecurity.oauth
spt-ds-oauth-server
1.0-SNAPSHOT
pom
spt-ds-oauth-server
UTF-8
UTF-8
1.8
junit
junit
4.13
test
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
javax.servlet
javax.servlet-api
3.1.0
provided
javax.interceptor
javax.interceptor-api
1.2
com.alibaba
fastjson
1.2.47
org.projectlombok
lombok
1.18.0
mysql
mysql-connector-java
5.1.47
org.springframework.security
spring-security-jwt
1.0.10.RELEASE
org.springframework.security.oauth.boot
spring-security-oauth2-autoconfigure
2.1.3.RELEASE
${project.name}
src/main/resources
true
**/*
src/main/java
**/*.xml
org.apache.maven.plugins
maven-compiler-plugin
1.8
maven-resources-plugin
utf-8
true
1.新new模块
2.起名称
4.0.0
com.ljf.springsecurity.oauth
spt-ds-oauth-server
1.0-SNAPSHOT
com.ljf.springsecurity.oauth
spt-ds-uaa-server
1.0-SNAPSHOT
spt-ds-uaa-server
junit
junit
4.13
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-openfeign
com.netflix.hystrix
hystrix-javanica
org.springframework.retry
spring-retry
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.data
spring-data-commons
org.springframework.cloud
spring-cloud-starter-security
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.security
spring-security-jwt
javax.interceptor
javax.interceptor-api
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
com.alibaba
fastjson
org.projectlombok
lombok
spring.application.name=uaa-service
server.port=53020
spring.main.allow-bean-definition-overriding = true
logging.level.root = debug
logging.level.org.springframework.web = info
spring.http.encoding.enabled = true
spring.http.encoding.charset = UTF-8
spring.http.encoding.force = true
server.tomcat.remote_ip_header = x-forwarded-for
server.tomcat.protocol_header = x-forwarded-proto
server.use-forward-headers = true
server.servlet.context-path = /uaa
spring.freemarker.enabled = true
spring.freemarker.suffix = .html
spring.freemarker.request-context-attribute = rc
spring.freemarker.content-type = text/html
spring.freemarker.charset = UTF-8
spring.mvc.throw-exception-if-no-handler-found = true
spring.resources.add-mappings = false
spring.datasource.url = jdbc:mysql://localhost:3306/security_db?useUnicode=true
spring.datasource.username = root
spring.datasource.password = mysql
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
eureka.client.serviceUrl.defaultZone = http://localhost:53000/eureka/
eureka.instance.preferIpAddress = true
eureka.instance.instance-id = ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
management.endpoints.web.exposure.include = refresh,health,info,env
feign.hystrix.enabled = true
feign.compression.request.enabled = true
feign.compression.request.mime-types[0] = text/xml
feign.compression.request.mime-types[1] = application/xml
feign.compression.request.mime-types[2] = application/json
feign.compression.request.min-request-size = 2048
feign.compression.response.enabled = true
package com.ljf.springsecurity.oauth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients(basePackages = {"com.ljf.springsecurity.oauth"})
public class UaaApp
{
public static void main( String[] args )
{
SpringApplication.run( UaaApp.class, args);
}
}
4.0.0
com.ljf.springsecurity.oauth
spt-ds-oauth-server
1.0-SNAPSHOT
com.ljf.springsecurity.oauth
spt-ds-order-server
1.0-SNAPSHOT
spt-ds-order-server
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.13
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-security
org.springframework.cloud
spring-cloud-starter-oauth2
javax.interceptor
javax.interceptor-api
com.alibaba
fastjson
org.projectlombok
lombok
spring.application.name=order-service
server.port=53021
spring.main.allow-bean-definition-overriding = true
logging.level.root = debug
logging.level.org.springframework.web = info
spring.http.encoding.enabled = true
spring.http.encoding.charset = UTF-8
spring.http.encoding.force = true
server.tomcat.remote_ip_header = x-forwarded-for
server.tomcat.protocol_header = x-forwarded-proto
server.use-forward-headers = true
server.servlet.context-path = /order
spring.freemarker.enabled = true
spring.freemarker.suffix = .html
spring.freemarker.request-context-attribute = rc
spring.freemarker.content-type = text/html
spring.freemarker.charset = UTF-8
spring.mvc.throw-exception-if-no-handler-found = true
spring.resources.add-mappings = false
eureka.client.serviceUrl.defaultZone = http://localhost:53000/eureka/
eureka.instance.preferIpAddress = true
eureka.instance.instance-id = ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
management.endpoints.web.exposure.include = refresh,health,info,env
feign.hystrix.enabled = true
feign.compression.request.enabled = true
feign.compression.request.mime-types[0] = text/xml
feign.compression.request.mime-types[1] = application/xml
feign.compression.request.mime-types[2] = application/json
feign.compression.request.min-request-size = 2048
feign.compression.response.enabled = true
package com.ljf.springsecurity.oauth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApp
{
public static void main( String[] args )
{
SpringApplication.run(OrderApp.class, args);
}
}