Spring security oauth2最简单入门环境搭建--二、干货

关于OAuth2的一些简介,见我的上篇blog: http://wwwcomy.iteye.com/blog/2229889 PS:貌似内容太水直接被鹳狸猿干沉。。

友情提示 学习曲线: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)
    • 网站B的client认证配置起作用了,AccessDecisionManager起作用了,资源信息起作用了,Token生成也起作用了。
    • 5.最后网站B通过access token访问资源,资源和资源的保护配置起作用了。


    具体的使用流程在appDemo的index页面有步骤。

    源码见 https://github.com/wwwcomy/appDemo/tree/OAuth2

    完。


    ----------------------20170907更新--------------------------

    POM已经更新spring-security-oauth2至2.2.0.RELEASE。
    Token已经使用JWT作为token type, 有个bug是在获取access token的时候token type还是bearer。没时间看源码了,因为。。。。

    因为用Spring Boot搭建一个OAuth2 Server竟然只需要20分钟。。 醉了 后续会补充一个

    猜你喜欢

    转载自wwwcomy.iteye.com/blog/2230265