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. 均方根误差(RMSE)与平均绝对误差(MAE)

    RMSE Root Mean Square Error,均方根误差 是观测值与真值偏差的平方和与观测次数m比值的平方根. 是用来衡量观测值同真值之间的偏差 MAE Mean Absolute Erro ...

  2. 高性能IO之Reactor模式

    The reactor design pattern is an event handling pattern for handling service requests delivered conc ...

  3. es集群数据库~原理细节

    ES原理一 基本定义  index(索引)  相当于mysql中的数据库  type(类型)  相当于mysql中的一张表  document(文档)  相当于mysql中的一行(一条记录)  fie ...

  4. Flask恋爱的一瞬间

    python的三大框架:Flask:轻量级框架 Django:重量级框架 Tornado:性能最好,异步框架 Flask初学 #导包 from flask import Flask #建立flask对 ...

  5. EasyGui的一个小例子

    EasyGui的安装:首先下载easyGui安装包,地址链接:https://pan.baidu.com/s/1D8f_eXWn7l8xhcTuEsqZmA 密码:e5z5 安装步骤: 1.进入eas ...

  6. iframe教程

    有关iframe的最强大的强大的教程 $(window.parent.document).contents().find("#tab_release"+taskId2+" ...

  7. 【php】随缘php企业网站管理系统V2.0 shownews.php注入漏洞

    程序名称:随缘网络php企业网站管理系统2.0免费版 以下为系统的功能简介: 1.采用div+css布局经测试兼容IE及firefox主流浏览器,其他浏览器暂未测试. 2.产品新闻三级无限分类. 3. ...

  8. Linker Scripts3--SECTIONS Command

    1.前言 SECTIONS命令告诉链接器如何映射输入段到输出段以及在内存中如何放置输出段,SECTIONS命令的格式如下: SECTIONS { sections-command sections-c ...

  9. Session、LocalStorage、SessionStorage、Cache-Ctrol比较

    1.Session Session是什么? 服务器通过 Set-Cookie给用户一个sessionIdsessionId对应 服务器 内的一小块内存每次用户访问服务器的时候,服务器就听过Sessio ...

  10. u3d 2D开发学习

    http://gad.qq.com/article/detail/45365 https://blog.csdn.net/s556699/article/details/55224830 UI层设计  ...