1.资源服务器

  1. package com.ruhuanxingyun.config;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.config.http.SessionCreationPolicy;
  7. import org.springframework.security.core.AuthenticationException;
  8. import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  10. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  11. import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
  12. import org.springframework.security.web.AuthenticationEntryPoint;
  13.  
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19.  
  20. @Configuration
  21. public class SecurityResourceConfig extends ResourceServerConfigurerAdapter {
  22.  
  23. @Override
  24. public void configure(HttpSecurity http) throws Exception {
  25. http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  26. .authorizeRequests().antMatchers("/api/1.0/**").access("#oauth2.hasScope('all')").and()
  27. .authorizeRequests().antMatchers("/public/**").permitAll().and()
  28. .authorizeRequests().antMatchers("/export/**").permitAll().and()
  29. .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
  30. }
  31.  
  32. @Override
  33. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  34. resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
  35. }
  36.  
  37. class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
  38. @Override
  39. public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws ServletException {
  40. Map<String, Object> map = new HashMap<>(9);
  41. Throwable cause = authException.getCause();
  42. if (cause instanceof InvalidTokenException) {
  43. map.put("code", 401);
  44. map.put("msg", "无效的token");
  45. } else {
  46. map.put("code", 401);
  47. map.put("msg", "访问此资源需要完全的身份验证");
  48. }
  49. map.put("data", authException.getMessage());
  50. map.put("success", false);
  51. map.put("path", request.getServletPath());
  52. map.put("timestamp", String.valueOf(System.currentTimeMillis()));
  53. response.setContentType("application/json");
  54. response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
  55. try {
  56. ObjectMapper mapper = new ObjectMapper();
  57. mapper.writeValue(response.getOutputStream(), map);
  58. } catch (Exception e) {
  59. throw new ServletException();
  60. }
  61. }
  62. }
  63.  
  64. }

OAuth2 token的更多相关文章

  1. Spring Security OAuth2 token权限隔离

    由于项目OAuth2采用了多种模式,授权码模式为第三方系统接入,密码模式用于用户登录,Client模式用于服务间调用, 所有不同的模式下的token需要用  @PreAuthorize("h ...

  2. OAuth2 Token 一定要放在请求头中吗?

    Token 一定要放在请求头中吗? 答案肯定是否定的,本文将从源码的角度来分享一下 spring security oauth2 的解析过程,及其扩展点的应用场景. Token 解析过程说明 当我们使 ...

  3. [认证授权] 2.OAuth2(续) & JSON Web Token

    0. RFC6749还有哪些可以完善的? 0.1. 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何 ...

  4. 使用Fiddler获取OAuth2认证的access token时候返回502

    微软动态CRM专家罗勇 ,回复322或者20190402可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 我这里Fiddler的Composer功能来获取OAuth2 认 ...

  5. [认证授权] 2.OAuth2授权(续) & JWT(JSON Web Token)

    1 RFC6749还有哪些可以完善的? 1.1 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何获得 ...

  6. OAuth2 .net MVC实现获取token

    OAuth2 的原理这里不多讲,可以看:https://www.cnblogs.com/icebutterfly/p/8066548.html 直奔主题:这里要实现的功能为,统计微软的Owin程序集实 ...

  7. OAuth2.0与前端无感知token刷新实现

    前言 OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛的应用.Facebook.Twitter和Google等各种在线服务都提供了基于OAuth规范的认证机制. ...

  8. spring security oauth2.0 实现

    oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...

  9. 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server

    原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...

随机推荐

  1. modbus 寄存器介绍

    modbus 的查询命令 命令 地址开始(两个地址)     地址长度(两个地址)          检验 01  xx     xx xx                              ...

  2. Linux中的libc和glibc

    现在centos6.8-x64系统里的c标准库已经成了glibc,glibc取代了libc,c标准库的位置在/lib64/libc.so.6 以下为转载 一.libc库 Linux平台提供的C标准库包 ...

  3. J - Joyful HDU - 5245 (概率)

    题目链接: J - Joyful  HDU - 5245 题目大意:给你一个n*m的矩阵,然后你有k次涂色机会,然后每一次可以选定当前矩阵的一个子矩阵染色,问你这k次用完之后颜色个数的期望. 具体思路 ...

  4. DeepLearning.ai-Week1-Convolution+model+-+Application

    1.0 - TensorFlow model 导入相关依赖包. import numpy as np import h5py import matplotlib.pyplot as plt impor ...

  5. Linux 学大纲

  6. delete 和 delete []的区别

    (1). 针对简单类型 使用new分配后的不管是数组还是非数组形式内存空间用两种方式均可 如:   int *a = new int[10];   delete a;   delete [] a;   ...

  7. python第l六天,lambda表达式学习,涉及filter及Map。

    在python中lambda表达式可以作为匿名函数来使用,举一个简单的栗子: 以前我们写两个数相加的函数需要 #以前我们写两个数相加的函数,需要这样写 >>> def sum(x,y ...

  8. swap扩展

    没有独立的分区,本地回环设备(使用软件来模拟实现硬件) 创建一个镜像文件 https://blog.csdn.net/linuxnews/article/details/51271875 有独立的分区

  9. 统计分析与R软件-chapter2-2

    2.2 数字.字符与向量 2.2.1 向量 1.向量的赋值 x<-c(10.4,5.6,3.1,6.4,21.7) 2.向量的运算 x<-c(-1,0,2);y<-c(3,8,2) ...

  10. 【转】python f-string

    [转]python f-string   文章目录 1. 主要内容 1.1. 旧时代的格式化字符串 1.1.1. Option #1: %-formatting 1.1.2. 怎样使用 %-forma ...