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

7e4e3d050cd96ac34a1467263d1379ff.png

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。

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

0dbe99fd03398c4e829f063a0336d742.png

三、资源服务器

资源服务器,顾名思义是保护资源的,在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服务化构建

喜欢这篇文章么,喜欢就加入我们一起讨论SpringCloud使用吧!

999b04f170bd33df544c7661e49e68f1.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值