OAuth2 token
1.资源服务器
- package com.ruhuanxingyun.config;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.http.SessionCreationPolicy;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
- import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
- import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
- import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
- import org.springframework.security.web.AuthenticationEntryPoint;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.Map;
- @Configuration
- public class SecurityResourceConfig extends ResourceServerConfigurerAdapter {
- @Override
- public void configure(HttpSecurity http) throws Exception {
- http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
- .authorizeRequests().antMatchers("/api/1.0/**").access("#oauth2.hasScope('all')").and()
- .authorizeRequests().antMatchers("/public/**").permitAll().and()
- .authorizeRequests().antMatchers("/export/**").permitAll().and()
- .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
- }
- @Override
- public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
- resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
- }
- class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
- @Override
- public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws ServletException {
- Map<String, Object> map = new HashMap<>(9);
- Throwable cause = authException.getCause();
- if (cause instanceof InvalidTokenException) {
- map.put("code", 401);
- map.put("msg", "无效的token");
- } else {
- map.put("code", 401);
- map.put("msg", "访问此资源需要完全的身份验证");
- }
- map.put("data", authException.getMessage());
- map.put("success", false);
- map.put("path", request.getServletPath());
- map.put("timestamp", String.valueOf(System.currentTimeMillis()));
- response.setContentType("application/json");
- response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
- try {
- ObjectMapper mapper = new ObjectMapper();
- mapper.writeValue(response.getOutputStream(), map);
- } catch (Exception e) {
- throw new ServletException();
- }
- }
- }
- }
OAuth2 token的更多相关文章
- Spring Security OAuth2 token权限隔离
由于项目OAuth2采用了多种模式,授权码模式为第三方系统接入,密码模式用于用户登录,Client模式用于服务间调用, 所有不同的模式下的token需要用 @PreAuthorize("h ...
- OAuth2 Token 一定要放在请求头中吗?
Token 一定要放在请求头中吗? 答案肯定是否定的,本文将从源码的角度来分享一下 spring security oauth2 的解析过程,及其扩展点的应用场景. Token 解析过程说明 当我们使 ...
- [认证授权] 2.OAuth2(续) & JSON Web Token
0. RFC6749还有哪些可以完善的? 0.1. 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何 ...
- 使用Fiddler获取OAuth2认证的access token时候返回502
微软动态CRM专家罗勇 ,回复322或者20190402可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 我这里Fiddler的Composer功能来获取OAuth2 认 ...
- [认证授权] 2.OAuth2授权(续) & JWT(JSON Web Token)
1 RFC6749还有哪些可以完善的? 1.1 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何获得 ...
- OAuth2 .net MVC实现获取token
OAuth2 的原理这里不多讲,可以看:https://www.cnblogs.com/icebutterfly/p/8066548.html 直奔主题:这里要实现的功能为,统计微软的Owin程序集实 ...
- OAuth2.0与前端无感知token刷新实现
前言 OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛的应用.Facebook.Twitter和Google等各种在线服务都提供了基于OAuth规范的认证机制. ...
- spring security oauth2.0 实现
oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...
- 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...
随机推荐
- modbus 寄存器介绍
modbus 的查询命令 命令 地址开始(两个地址) 地址长度(两个地址) 检验 01 xx xx xx ...
- Linux中的libc和glibc
现在centos6.8-x64系统里的c标准库已经成了glibc,glibc取代了libc,c标准库的位置在/lib64/libc.so.6 以下为转载 一.libc库 Linux平台提供的C标准库包 ...
- J - Joyful HDU - 5245 (概率)
题目链接: J - Joyful HDU - 5245 题目大意:给你一个n*m的矩阵,然后你有k次涂色机会,然后每一次可以选定当前矩阵的一个子矩阵染色,问你这k次用完之后颜色个数的期望. 具体思路 ...
- DeepLearning.ai-Week1-Convolution+model+-+Application
1.0 - TensorFlow model 导入相关依赖包. import numpy as np import h5py import matplotlib.pyplot as plt impor ...
- Linux 学大纲
- delete 和 delete []的区别
(1). 针对简单类型 使用new分配后的不管是数组还是非数组形式内存空间用两种方式均可 如: int *a = new int[10]; delete a; delete [] a; ...
- python第l六天,lambda表达式学习,涉及filter及Map。
在python中lambda表达式可以作为匿名函数来使用,举一个简单的栗子: 以前我们写两个数相加的函数需要 #以前我们写两个数相加的函数,需要这样写 >>> def sum(x,y ...
- swap扩展
没有独立的分区,本地回环设备(使用软件来模拟实现硬件) 创建一个镜像文件 https://blog.csdn.net/linuxnews/article/details/51271875 有独立的分区
- 统计分析与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) ...
- 【转】python f-string
[转]python f-string 文章目录 1. 主要内容 1.1. 旧时代的格式化字符串 1.1.1. Option #1: %-formatting 1.1.2. 怎样使用 %-forma ...