spring security oauth2 资源服务器配置
2019独角兽企业重金招聘Python工程师标准>>>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
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.token.RemoteTokenServices;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;/*** 资源服务配置** @author Canaan* @date 2018/10/29 14:57*/@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {@Autowiredprivate LogoutSuccessHandler logoutSuccessHandler;@Autowiredprivate AuthenticationEntryPoint denialAuthenticationEntryPoint;@Primary@Beanpublic RemoteTokenServices tokenServices() {final RemoteTokenServices tokenService = new RemoteTokenServices();tokenService.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");tokenService.setClientId("risk");tokenService.setClientSecret("risk123456");return tokenService;}@Overridepublic void configure(HttpSecurity http) throws Exception {http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);http.csrf().disable();http.exceptionHandling().authenticationEntryPoint(denialAuthenticationEntryPoint);http.logout().logoutUrl("/oauth/logout").logoutSuccessHandler(this.logoutSuccessHandler);http.authorizeRequests().anyRequest().authenticated();}@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {resources.resourceId("risk").stateless(true);}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;/*** @author Canaan* @date 2018/10/26 18:04*/
@Component
public class DenialAuthenticationEntryPoint implements AuthenticationEntryPoint {private final Logger logger = LoggerFactory.getLogger(DenialAuthenticationEntryPoint.class);@Overridepublic void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {logger.info("拒绝访问!!!");response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied");}}
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;/*** @author Canaan* @date 2018/10/29 15:18*/
@Component
public class MyLogoutSuccessHandle implements LogoutSuccessHandler {private final Logger logger = LoggerFactory.getLogger(MyLogoutSuccessHandle.class);@Autowiredprivate TokenStore tokenStore;@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {Optional<OAuth2AccessToken> tokenOptional = getToken(request);if (!tokenOptional.isPresent()) {logger.error("access token 获取失败");return;}tokenStore.removeAccessToken(tokenOptional.get());logger.debug("成功退出");}private Optional<OAuth2AccessToken> getToken(HttpServletRequest request) {//从参数中获取String token = request.getParameter("access_token");if (StringUtils.isNotBlank(token)) {OAuth2AccessToken oAuth2AccessToken = this.tokenStore.readAccessToken(token.trim());if (oAuth2AccessToken != null) {return Optional.of(oAuth2AccessToken);}}//从头部中获取token = request.getHeader("authorization");if (StringUtils.isBlank(token)) {return Optional.empty();}String[] arr = StringUtils.split(token, " ");if (arr == null || arr.length != 2) {return Optional.empty();}token = arr[1];OAuth2AccessToken oAuth2AccessToken = this.tokenStore.readAccessToken(token.trim());return Optional.ofNullable(oAuth2AccessToken);}}
转载于:https://my.oschina.net/u/2552286/blog/2254126
spring security oauth2 资源服务器配置相关推荐
- 从零开始超详细的Spring Security OAuth2.0实现分布式系统授权(注册中心+网关+认证授权服务(JWT令牌验证)+资源调用服务)
文章目录 一.OAuth2.0 1.介绍 2.例子 3.执行流程 二.Spring Cloud Security OAuth2 1.环境介绍 2.认证流程 三.整合分布式项目 1.技术方案 2.项目结 ...
- Spring Security Oauth2 在资源服务器如何获取jwt中的额外信息
在上一篇Spring Security Oauth2 中实现TokenEnhancer向jwt中添加额外信息中,笔者向大家介绍了如何向jwt中存额外信息(extraInfo) 接下来这篇blog,笔者 ...
- 【Spring】Spring Security OAuth2 JWT 认证
1.概述 Spring Security OAuth2 JWT 认证服务器配置 Spring Security OAuth2 JWT 资源服务器配置 Spring Security OAuth2 Re ...
- 还不了解Oauth2协议?这篇文章从入门到入土让你了解Oauth2以及Spring Security OAuth2 的使用
SpringSecurityOAuth2学习和实战 1.OAuth2概述 1.1 什么是OAuth2 OAuth(Open Authorization)是一个关于授权(authorization)的开 ...
- Spring Security OAuth2.0认证授权知识概括
Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...
- 【Spring Cloud Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权
一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间这里只贴出关键部分代码的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证 ...
- 芋道 Spring Security OAuth2 入门
芋道 Spring Security OAuth2 入门 总阅读量:28123次 <Dubbo 实现原理与源码解析 -- 精品合集> <Netty 实现原理与源码解析 -- 精品合集 ...
- JWT实战 Spring Security Oauth2整合JWT 整合SSO单点登录
文章目录 一.JWT 1.1 什么是JWT 1.2 JWT组成 头部(header) 载荷(payload) 签名(signature) 如何应用 1.3 JJWT 快速开始 创建token toke ...
- spring security oauth2 常用授权方式配置详细教程(一)
1 spring security oauth2 简单配置说明(一) 配套源码:https://download.csdn.net/download/tiancxz/12902941 1.1 工程说明 ...
最新文章
- 通过声明Attribute属性改变不同类的输出效果
- 《Linux内核原理与分析》第三周作业
- 文件包含——apache日志文件包含shell(四)
- linux动态库与静态库混合连接
- JS中使用bignumber处理高精度小数运算
- 2017/08/22 工作日志
- mysql 自然排序_如何在mysql中实现自然排序
- jemalloc内存分配器简介
- 路径取值之含正则表达式的的取值方法
- 《C语言及程序设计》实践参考——体重监测器
- jQuery-1.9.1源码分析系列(十) 事件系统——事件体系结构
- ckeditor5富文本数学化学方程式
- 计算机用户最高权限,win7系统开启用户最高管理权限的方法
- 从接口、抽象类到工厂模式再到JVM来总结一些问题
- Csdn富文本编辑器中使用Emoji表情包
- HJL-93/AY AC220V数字式交流三相电流继电器
- PHPwind9.01傻瓜图解安装教程
- xcode 软件˙∆集~
- 80个自我提升的网站
- 百度振兴计划:中国版ChatGPT“狂飙”的机遇与挑战
热门文章
- 学习指南!美国java程序员要求
- 年末阿里百度等大厂技术面试题汇总,完整版开放下载
- 计算机维护宝典,超齐全的维修宝典之电脑维修实例大全
- php函数总结,php函数
- unity 当前移动方向_Unity小工具:溶解效果(Dissolve)
- enityframework 已连接的当前状态为打开。_蓝牙连接有问题吗?尝试针对macOS的以下6个修复程序
- php+mysql多事务处理_php+mysql事务处理
- 图像视频信息库改直播服务器,短视频直播系统,开发流程详细解析
- 学会python能做兼职工作无押金_各位第3期Python精英班的同学注意啦,你们找工作可要认真!...
- lamp 配置mysql_LAMP安装配置超详细讲解