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的更多相关文章

  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. 【blog】mysql字段类型datetime和timestamp的区别

    首先 DATETIM和TIMESTAMP类型所占的存储空间不同,前者8个字节,后者4个字节,这样造成的后果是两者能表示的时间范围不同.前者范围为1000-01-01 00:00:00 ~ 9999-1 ...

  2. Mysql大文本类型

    TEXT 一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符. MEDIUMTEXT 一个BLOB或TEXT列,最大长度为16777215(2^24-1)个字符. LONGTEXT ...

  3. jQuery(二)button事件改变标签值

    button事件改变标签值 例子一: <html> <head> <script type="text/javascript" src="/ ...

  4. mysql 案例 ~ 主从复制延迟之并行复制

    一 概念说明   1 模型 并行复制是典型的生产者.消费者模式,Coordinator作为生产者,worker线程作为消费者.   2 Waiting for preceding transactio ...

  5. nginx 目录讲解

  6. C#解析"a=1&b=2&c=3"字符串,微信支付返回字符串,替换<br>为&

    原文来自: http://www.mzwu.com/article.asp?id=2802 C#可用: 若该字符串是使用Http Get发送,url?a=1&b=2&c=3,使用下边代 ...

  7. 【转】实习小记-python中可哈希对象是个啥?what is hashable object in python?

    [转]实习小记-python中可哈希对象是个啥?what is hashable object in python? 废话不多说直接祭上python3.3x的文档:(原文链接) object.__ha ...

  8. Linux查看压缩文件内容【转】

    查看一个归档或者压缩文件的内容而无需解压它 得益于 Linux 社区,有很多命令行工具可以来达成上面的目标.下面就让我们来看看使用它们的一些示例. 1.使用 vim 编辑器 vim 不只是一个编辑器, ...

  9. ubuntu 安装 eclipse 及其CDT

    CDT是在eclipse平台上进行c/c++程序开发的插件.首先安装eclipse平台. 1.在Ubuntu 16.04上查看 eclipse是否已经安装: eclipse 若已经安装,则会进入到ec ...

  10. Centos7.3_x86_64通过systemctl控制tomcat8.0.46启动和停止

    Centos7.3_x86_64通过systemctl控制tomcat8..46启动和停止 之前在centos 6上通过脚本控制tomcat 启动和停止的脚本,虽然在centos 7也可以使用,但ce ...