Spring security oauth2最简单入门环境搭建
友情提示 学习曲线:spring+spring mvc+spring security+Oauth2基本姿势,如果前面都没看过请及时关闭本网页。
我有信心我的这个blog应该是迄今为止使用spring security oauth2最简单的hello world app介绍了,如果你下下来附件源码还看不懂,请留言。。
其他能搜到的如http://blog.csdn.net/monkeyking1987/article/details/16828059这个哥们儿
和
http://www.cnblogs.com/smarterplanet/p/4088479.html
这个,都很好,不过因为依赖了数据库,配置比较多,对于初学者来说,搭起来一个最最简单的环境才是最重要的,撸要一步一步走嘛(如果对spring不是特别熟,强烈不建议看官方的tutorial,槽点太多,例如静态JS库的引用方式,web.xml的使用方式,Spring的配置方式,我反正下下来就惊呆了,第一次看到用java代码配spring)
基本需求:
用户A希望授权网站B能获取自己在网站appDemo的一个资源,资源的地址是
http://localhost:8080/appDemo/resource/getUserInfo
使用的框架及版本:
- spring-webmvc 3.2.8
- spring-security-web 3.2.6
- spring-security-oauth2 2.0.7 (这是到2015.7为止的最新版本,和1.0有些小区别,我也在这上面卡了一段时间)
Pom文件见附件整个项目源码。
准备材料(附件都已经包括):
- 搭建一个springMVC+springSecurity的最简单环境,并且自定义一个login.jsp
- 写一个controller和Jsp,充当用户的受保护资源
- 写两个jsp作为scope选择确认页面
我们要做的
仅仅是配置spring security的配置文件就可以了~一点代码都不用写,数据库也不用连。
我会从client信息开始,把整个配置文件串起来,最后我会讲appDemo使用的方法,和以下的配置联系起来
- 网站B在网站appDemo的"用户"(注意这里和用户A没半毛钱关系)信息,即client_id和client_secret配置:
- <authentication-manager id="clientAuthenticationManager">
- <authentication-provider user-service-ref="oauth2ClientDetailsUserService" />
- </authentication-manager>
- <beans:bean id="oauth2ClientDetailsUserService"
- class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
- <beans:constructor-arg ref="clientDetailsService" />
- </beans:bean>
- <oauth2:client-details-service id="clientDetailsService">
- <oauth2:client client-id="m1"
- authorized-grant-types="password,authorization_code,refresh_token,implicit"
- secret="s1" scope="read,write,trust" authorities="ROLE_CLIENT, ROLE_TRUSTED_CLIENT"
- resource-ids="pic-resource" />
- </oauth2:client-details-service>
注意命名空间,可以看到client配置和spring security传统的用户配置是非常像的。
这里配置了网站B在appDemo中的client_id,client_secret,scope,权限和授权类型,资源ID,那这个资源ID是个啥呢?
- 资源filter配置:
- <oauth2:resource-server id="picResourceServer"
- resource-id="pic-resource" token-services-ref="tokenServices" />
配置这个,除了资源定位(id),还为了给我们的资源加上一个自定义的OAuth过滤器,这个过滤器主要是为了网站B在访问资源的时候验证Access Token。然后出现了两个分支:
1.配token service
2.配资源
- 先讲Token配置:
- <beans:bean id="tokenServices"
- class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
- <beans:property name="tokenStore" ref="tokenStore" />
- <beans:property name="supportRefreshToken" value="true" />
- <beans:property name="clientDetailsService" ref="clientDetailsService" />
- </beans:bean>
- <beans:bean id="tokenStore"
- class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore">
- </beans:bean>
我们把Token存在内存里,摆脱数据库依赖。想象一下,为了生成和client相关的token,肯定需要把上面提到的ClientService配置进去
- 资源和资源的保护
- <http pattern="/resource/**" create-session="never"
- entry-point-ref="oauth2AuthenticationEntryPoint"
- access-decision-manager-ref="oauth2AccessDecisionManager">
- <anonymous enabled="false" />
- <intercept-url pattern="/resource/**" access="ROLE_USER,SCOPE_READ" />
- <custom-filter ref="picResourceServer" before="PRE_AUTH_FILTER" />
- <access-denied-handler ref="oauthAccessDeniedHandler" />
- </http>
这是spring security的传统配置了,除了我们刚才提到的资源filter,还配置了认证失败、授权失败的处理,和判断是否可访问的"access decision manager"
- 认证失败,授权失败
- <beans:bean id="oauth2AuthenticationEntryPoint"
- class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint" />
- <beans:bean id="oauthAccessDeniedHandler"
- class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />
这个没什么好解释的了,其实作为demo也可以不配,毕竟我们应该会一路按能跑通的先跑。
- access decision manager
- <beans:bean id="oauth2AccessDecisionManager"
- class="org.springframework.security.access.vote.UnanimousBased">
- <beans:constructor-arg>
- <beans:list>
- <beans:bean
- class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
- <beans:bean class="org.springframework.security.access.vote.RoleVoter" />
- <beans:bean
- class="org.springframework.security.access.vote.AuthenticatedVoter" />
- </beans:list>
- </beans:constructor-arg>
- </beans:bean>
也是传统的配置方法了,多了个oauth2里面特有的scope投票机制。
好,到现在为止,OAuth2 Server所需要的所有龙珠都已经集齐,接下来就可以召唤神龙了
- 出来吧!神龙
- <oauth2:authorization-server
- client-details-service-ref="clientDetailsService" token-services-ref="tokenServices"
- user-approval-handler-ref="oauthUserApprovalHandler"
- user-approval-page="oauth_approval" error-page="oauth_error">
- <oauth2:authorization-code />
- <oauth2:implicit />
- <oauth2:refresh-token />
- <oauth2:client-credentials />
- <oauth2:password />
- </oauth2:authorization-server>
- <beans:bean id="oauthUserApprovalHandler"
- class="org.springframework.security.oauth2.provider.approval.DefaultUserApprovalHandler" />
还差两步:
1.网站B来申请Token时候的认证和权限设置:
- <http pattern="/oauth/token" create-session="stateless"
- authentication-manager-ref="clientAuthenticationManager"
- entry-point-ref="oauth2AuthenticationEntryPoint">
- <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
- <anonymous enabled="false" />
- <http-basic entry-point-ref="oauth2AuthenticationEntryPoint" />
- <custom-filter ref="clientCredentialsTokenEndpointFilter"
- before="BASIC_AUTH_FILTER" />
- <access-denied-handler ref="oauthAccessDeniedHandler" />
- </http>
- <beans:bean id="clientCredentialsTokenEndpointFilter"
- class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
- <beans:property name="authenticationManager" ref="clientAuthenticationManager" />
- </beans:bean>
2.作为屌丝用户A,认证放在配置最后了(也应该放在前面那个http的后面,因为这里有个全局匹配了)
- <http access-denied-page="/login.jsp?error=true"
- authentication-manager-ref="authenticationManager">
- <intercept-url pattern="/oauth/**" access="ROLE_USER" />
- <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
- <form-login login-page="/login.jsp"
- authentication-failure-url="/login.jsp?error=true"
- default-target-url="/index.jsp" />
- <anonymous />
- </http>
- <authentication-manager alias="authenticationManager">
- <authentication-provider>
- <user-service id="userService">
- <user name="admin" password="admin" authorities="ROLE_USER" />
- </user-service>
- </authentication-provider>
- </authentication-manager>
写到这里,我默默预览了一下,估计群众们看到这句话大都是拖滚轴下来的。
不过配置真的挺长,不花篇幅真讲不清楚。
最后把大致流程和配置关联一下:
- *.用户要授权网站B获取appDemo资源
- 1.网站B把用户跳转到appDemo/oauth/authorize?client_id=m1&redirect_uri=http%3a%2f%2flocalhost%3a8080%2f&response_type=code&scope=read
- Spring Security Oauth2有一个controller:AuthorizationEndpoint处理这个请求,这个是不用配置的。 但是在controller处理之前,appDemo发现,我擦嘞,用户还没登录啊(见屌丝用户A认证配置)!于是先跳转到登录界面让用户登录。
- 2.用户登录成功后,跳转到了scope选择确认页面(见出现吧!神龙)。
- 3.appDemo生成了Authorization Code并跳转回网站B(其实神龙的authorization-code这种配置方式有其他的配置项,可以选填Authorzation code service)
- 4.网站B拿到了code,向appDemo请求accessToken(appDemo/oauth/token?code=g6hW13&client_id=m1&client_secret=s1&grant_type=authorization_code&redirect_uri=http%3a%2f%2flocalhost%3a8080%2f)
- 5.最后网站B通过access token访问资源,资源和资源的保护配置起作用了。
网站B的client认证配置起作用了,AccessDecisionManager起作用了,资源信息起作用了,Token生成也起作用了。
具体的使用流程在appDemo的index页面有步骤。
完。
- appDemo.rar (7.9 KB)
- 下载次数: 938
Spring security oauth2最简单入门环境搭建的更多相关文章
- 使用Spring Security OAuth2进行简单的单点登录
1.概述 在本教程中,我们将讨论如何使用Spring Security OAuth和Spring Boot实现SSO - 单点登录. 我们将使用三个单独的应用程序: 授权服务器 - 这是中央身份验证机 ...
- Spring security + oauth2.0 + redis + mybatis plus 搭建微服务
上个星期一个朋友请求帮忙,让我搭建一个分布式授权中心的微服务,之前我也没搭建过,在网上撸了几天前辈们写的技术博客,搞出个模型,分享给大家: 前辈们博客地址: OAuth2.0 原理:https://b ...
- spring security oauth2 搭建认证中心demo
oauth2 介绍 oauth2 协议应该是开发者们耳熟能详的协议了,这里就不做过多的介绍了,具体介绍如何在spring security中搭建oauth2的认证服务.Spring-Securit ...
- spring security oauth2搭建resource-server demo及token改造成JWT令牌
我们在上文讲了如何在spring security的环境中搭建基于oauth2协议的认证中心demo:https://www.cnblogs.com/process-h/p/15688971.html ...
- Spring Security OAuth2.0认证授权二:搭建资源服务
在上一篇文章[Spring Security OAuth2.0认证授权一:框架搭建和认证测试](https://www.cnblogs.com/kuangdaoyizhimei/p/14250374. ...
- springboot+spring security +oauth2.0 demo搭建(password模式)(认证授权端与资源服务端分离的形式)
项目security_simple(认证授权项目) 1.新建springboot项目 这儿选择springboot版本我选择的是2.0.6 点击finish后完成项目的创建 2.引入maven依赖 ...
- Re:从零开始的Spring Security Oauth2(一)
前言 今天来聊聊一个接口对接的场景,A厂家有一套HTTP接口需要提供给B厂家使用,由于是外网环境,所以需要有一套安全机制保障,这个时候oauth2就可以作为一个方案. 关于oauth2,其实是一个规范 ...
- Spring Security Oauth2系列(一)
前言: 关于oauth2,其实是一个规范,本文重点讲解spring对他进行的实现,如果你还不清楚授权服务器,资源服务器,认证授权等基础概念,可以移步理解OAuth 2.0 - 阮一峰,这是一篇对于oa ...
- Spring Security OAuth2 Demo -- good
1. 添加依赖授权服务是基于Spring Security的,因此需要在项目中引入两个依赖: <dependency> <groupId>org.springframework ...
随机推荐
- RUBY的类封装,继承,多态简单演示
class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end def t ...
- Linux下根据进程的名字杀死进程
以前是写了一个bash,通过ps,grep 和awk配合搜索PID再kill掉进程.果然以前傻,不知道解决问题之前先google,原来直接就有相关的pkill -f "process_nam ...
- Java正则表达式的语法与示例
Java正则表达式的语法与示例 java 正则表达式 正则表达式语法 java正则表达式语法 java正则表达式 概要: Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 ...
- Generating SSH Keys [Ubuntu Linux]
Generating SSH Keys We strongly recommend using an SSH connection when interacting with GitHub. SSH ...
- Mitsubish FX 3U PLC 串口 连接单元
前段时间遇到一个Mitsubish FX 3U PLC ,现将PLC连接单元分享一下,希望对其他人有所启示. unit PLC_MitsubishiFX; interface uses Windows ...
- RHEL修改主机名和IP
1, 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=NEWHOSTNAME #修改该值作为主机名,如:NEWPC ...
- Groovy简洁开发,我用到的简洁之处
最近一直在用Groovy开发以前的项目,一边学习一边开发,工具用的是IDEA(欲哭无泪,不熟悉真是搞死人).......由于我做的是服务层,是为公司其它项目做服务支撑的,所以就没有用框架,只有一些se ...
- repo的小结
repo仅仅是google用Python脚本写的调用git的一个脚本,主要是用来下载.管理Android项目的软件仓库. 1. 下载 repo 的地址: http://android.git.kern ...
- [Angular 2] implements OnInit, OnDestory for fetching data from server
Link: https://angular.io/docs/js/latest/api/core/OnInit-interface.html, https://www.youtube.com/watc ...
- Linux驱动开发常用头文件
头文件目录中总共有32个.h头文件.其中主目录下有13个,asm子目录中有4个,linux子目录中有10个,sys子目录中有5个.这些头文件各自的功能如下: 1.主目录 <a.out.h> ...