在《SpringBoot入门建站全系列(三十五)整合Oauth2做单机版认证授权》和《Spring整合Oauth2单机版认证授权详情》中
已经介绍了如何单机整合oauth2,但是单机使用只是自己测试而已,实际部署差别还是蛮大。
本篇就是对Oauth2的实际应用方法做详细的结束,并介绍腾讯是怎么用oauth2来做应用授权的。
如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。
在Spring Oauth2中,Oauth2的使用过程中将角色分为三种:ResourceServer,AuthorizationServer,OauthClient.
由于篇幅较大,这里将Oauth2的搭建分成三个部分。本篇先介绍ResourceServer。
首发地址:SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资源服务器
Oauth2有四种模式,但是使用最广泛的是授权码模式(authorization code):
整个授权过程需要经历:登录–》授权–》获取code–》换取access_token。
下图是找到的一个比较完整的流程图:
资源服务器,顾名思义是保护资源的,在Oauth2中,客户端往往也是资源服务器,各个服务之间的通信需携带访问令牌。
从实现上来看,只要加上@EnableResourceServer,并继承ResourceServerConfigurerAdapter写出自己的实现,你的资源服务器就搭成了。
下面讲述下这个过程是怎样的。
需要引入spring-boot-starter-web、spring-cloud-starter-security和spring-security-oauth2-autoconfigure.
依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.security.oauth.bootgroupId>
<artifactId>spring-security-oauth2-autoconfigureartifactId>
<version>${security.oauth2.version}version>
dependency>
dependencies>
这里,security.oauth2.version我选择的是2.1.6.RELEASE。
这里使用yaml文件写配置,配置文件application.yml:
application.yml:
server:
port: 8183
spring:
profiles:
active: loc
application:
name: oauthResource
security:
oauth2:
resource:
tokenInfoUri: http://sso.pomit.cn/oauth/check_token
preferTokenInfo: true
client:
clientId: ResourceServer
clientSecret: 123
这里,我的loc配置文件为空,就不显示了。
使用main直接启动即可。需要使用注解@EnableResourceServer表明它是资源服务器。
OauthResourceApplication :
package cn.pomit.springbootwork.oauthresource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@EnableResourceServer
@SpringBootApplication
public class OauthResourceApplication {
public static void main(String[] args) {
SpringApplication.run(OauthResourceApplication.class, args);
}
}
需要继承ResourceServerConfigurerAdapter. 并对资源路径进行安全配置。其中accessDeniedHandler需要指定为OAuth2AccessDeniedHandler。
依赖如下:
package cn.pomit.springbootwork.oauthresource.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/test/**").permitAll()
.antMatchers("/").permitAll()
.antMatchers("/index.html").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/img/**").permitAll()
.anyRequest().authenticated().and()
.exceptionHandling()
.accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
下面写几个测试的web。
OauthResourceRest:
package cn.pomit.springbootwork.oauthresource.web;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.pomit.springbootwork.oauthresource.model.IpModel;
import cn.pomit.springbootwork.oauthresource.model.ResultModel;
import cn.pomit.springbootwork.oauthresource.util.IPUtil;
@RestController
@RequestMapping("/api")
public class OauthResourceRest {
@RequestMapping(value = "/ip", method = { RequestMethod.GET })
public ResultModel ip(HttpServletRequest request) {
IpModel ipModel = new IpModel();
ipModel.setClientIpAddress(IPUtil.getIpAddr(request));
ipModel.setServerIpAddress(IPUtil.localIp());
return ResultModel.ok(ipModel);
}
@RequestMapping(value = "/userInfo", method = { RequestMethod.GET })
public ResultModel userinfo(Principal principal) {
return ResultModel.ok(principal.getName());
}
}
OauthTestRest :
package cn.pomit.springbootwork.oauthresource.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.pomit.springbootwork.oauthresource.model.ResultModel;
@Controller
@RequestMapping("/")
public class OauthTestRest {
@ResponseBody
@RequestMapping(value = "/test/test", method = { RequestMethod.GET })
public ResultModel test() {
return ResultModel.ok("我就是test");
}
@RequestMapping(value = "/", method = { RequestMethod.GET })
public String index() {
return "/index.html";
}
}
IPUtil:
package cn.pomit.springbootwork.oauthresource.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
public class IPUtil {
/**
* @Description: 获取客户端IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (ip.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
ip = inet.getHostAddress();
}
}
// 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ip != null && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
/**
* 获取的是本地的IP地址
*
* @return
*/
public static String localIp() {
String result = "";
try {
InetAddress address = InetAddress.getLocalHost();
result = address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return result;
}
}
IpModel:
package cn.pomit.springbootwork.oauthresource.model;
public class IpModel {
private String clientIpAddress;
private String serverIpAddress;
public String getClientIpAddress() {
return clientIpAddress;
}
public void setClientIpAddress(String clientIpAddress) {
this.clientIpAddress = clientIpAddress;
}
public String getServerIpAddress() {
return serverIpAddress;
}
public void setServerIpAddress(String serverIpAddress) {
this.serverIpAddress = serverIpAddress;
}
}
ResultModel:
package cn.pomit.springbootwork.oauthresource.model;
/**
* @author cff
*/
public class ResultModel {
private String errorCode;
private String message;
private Object data;
public ResultModel() {
}
public ResultModel(String errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
public ResultModel(String errorCode, String message, Object data) {
this.errorCode = errorCode;
this.message = message;
this.data = data;
}
public String getErrorCode() {
return errorCode;
}
public void set ErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static ResultModel ok(String testValue) {
ResultModel rm = new ResultModel();
rm.setData(testValue);
return rm;
}
}
品茗IT-博客专题:https://www.pomit.cn/lecture.html汇总了Spring专题、Springboot专题、SpringCloud专题、web基础配置专题。
Spring项目快速开发工具:
一键快速构建Spring项目工具
一键快速构建SpringBoot项目工具
一键快速构建SpringCloud项目工具
一站式Springboot项目生成
Mysql一键生成Mybatis注解Mapper
Spring组件化构建
SpringBoot组件化构建
SpringCloud服务化构建