前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资源服务器

SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资源服务器

作者头像
品茗IT
发布2020-05-28 16:27:54
7590
发布2020-05-28 16:27:54
举报
文章被收录于专栏:品茗IT品茗IT品茗IT

SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资源服务器

一、概述

《SpringBoot入门建站全系列(三十五)整合Oauth2做单机版认证授权》《Spring整合Oauth2单机版认证授权详情》

已经介绍了如何单机整合oauth2,但是单机使用只是自己测试而已,实际部署差别还是蛮大。

本篇就是对Oauth2的实际应用方法做详细的结束,并介绍腾讯是怎么用oauth2来做应用授权的。

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

在Spring Oauth2中,Oauth2的使用过程中将角色分为三种:ResourceServer,AuthorizationServer,OauthClient.

由于篇幅较大,这里将Oauth2的搭建分成三个部分。本篇先介绍ResourceServer。

二、Oauth2的授权流程

Oauth2有四种模式,但是使用最广泛的是授权码模式(authorization code):

  1. 这种模式算是正宗的oauth2的授权模式
  2. 设计了auth code,通过这个code再获取token
  3. 支持refresh token

整个授权过程需要经历:登录--》授权--》获取code--》换取access_token。

下图是找到的一个比较完整的流程图:

在这里插入图片描述

三、资源服务器

资源服务器,顾名思义是保护资源的,在Oauth2中,客户端往往也是资源服务器,各个服务之间的通信需携带访问令牌。

从实现上来看,只要加上@EnableResourceServer,并继承ResourceServerConfigurerAdapter写出自己的实现,你的资源服务器就搭成了。

下面讲述下这个过程是怎样的。

3.1 引入依赖

需要引入spring-boot-starter-web、spring-cloud-starter-security和spring-security-oauth2-autoconfigure.

依赖如下:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-security</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.security.oauth.boot</groupId>
		<artifactId>spring-security-oauth2-autoconfigure</artifactId>
		<version>${security.oauth2.version}</version>
	</dependency>
</dependencies>

这里,security.oauth2.version我选择的是2.1.6.RELEASE。

3.2 配置文件

这里使用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配置文件为空,就不显示了。

  1. 应用名称为oauthResource
  2. security.oauth2.resource.tokenInfoUri是指向了授权服务器的检查token路径。这个等你自己部署了授权服务器,换掉地址就行了。
  3. security.oauth2.client是配置资源服务器的密钥,以便授权服务器能够试别该资源服务器。
3.3 启动

使用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);
	}
}
3.4 安全控制配置

需要继承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());
	}

}
3.5 测试web(以下都可以略过)

下面写几个测试的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服务化构建

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资源服务器
  • 一、概述
  • 二、Oauth2的授权流程
  • 三、资源服务器
    • 3.1 引入依赖
      • 3.2 配置文件
        • 3.3 启动
          • 3.4 安全控制配置
            • 3.5 测试web(以下都可以略过)
            • 快速构建项目
            相关产品与服务
            访问管理
            访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档