• 【JavaEE】SSH+Spring Security+Spring oauth2整合及example


    现在加最后一样,就是oauth2,现在很多网站都有对应的移动版本,那么移动端访问服务端的服务怎么控制权限,我知道的主要是两种方法,第一是模拟浏览器,访问服务的时候会生成session,之后在移动端缓存cookie,每次网络请求都把cookie加上,还有一种就是通过oauth2,登录之后生成一个凭证,每次请求时携带凭证,当然oauth2更多的是为第三方应用提供访问自己服务的权限。

    oauth2的配置,可以纯配置文件打造,相比较前面的那些,可以说是最简单也是最复杂的,简单是因为引入jar包配置一个xml就可以,复杂是说这个仅有的xml需要写的东西很多理解起来也要费劲。

    关于Oauth2的整合,很大程度上参考了这位仁兄的文章:http://blog.csdn.net/monkeyking1987/article/details/16828059

    1. pom.xml

    首先导入oauth2的包:

    <properties>
        ……
        <spring-security-oauth2.version>2.0.2.RELEASE</spring-security-oauth2.version>
    </properties>
    
    <dependencies>
        ……
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>${spring-security-oauth2.version}</version>
        </dependency>
        ……
    </dependencies>

    2. applicationContext-security.xml

    oauth2是security的一部分,配置也有关联,就不再单建文件,首先要在最前面加一些schema:

    <beans:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:beans="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/security/oauth2
                            http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
                            http://www.springframework.org/schema/security
                            http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    其实就是加了一个oauth2的命名。

    在这个文件的开始,也就是之前配置的拦截链的http标签前面并列一个http标签:

    <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="oauth2AuthenticationManager"  
              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="oauth2AccessDeniedHandler"/>  
    </http>

    这个标签处理/oauth/token的网络请求,这是oauth2的登录验证请求,那么登录需要什么,首先,和Spring Security一样,需要一个认证管理器,Spring Oauth2需要两个认证管理器,第一个就是之前Spring中配置的那一个,用来验证用户名密码的,还有一个是用来区分客户端用户的,给它起个名字叫oauth2AuthenticationManager:

    <oauth2:client-details-service id="clientDetailsService">
        <oauth2:client client-id="mobile_1" authorized-grant-types="password,authorization_code,refresh_token,implicit"
                    secret="secret_1"scope="read,write,trust" />
    </oauth2:client-details-service>
    <beans:bean id="oauth2ClientDetailsUserService"
                    class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <beans:constructor-arg ref="clientDetailsService"/>
    </beans:bean>
    <authentication-manager id="oauth2AuthenticationManager">
        <authentication-provider user-service-ref="oauth2ClientDetailsUserService"/>
    </authentication-manager>

    这儿设置了一种客户端,id叫做mobile_1,secret叫做secret_1,针对read、write和trust几个域有效。这几个域会在访问控制中被用到。

    当登录成功之后会得到一个token,再次访问的时候需要携带这个token,spring-oauth2根据这个token来做认证,那么spring-oauth2必须先存一份token和用户关系的对应,因为不用session了,这就相当于session,那么这个token在服务器中怎么存,有两种主要的存储方式,一是创建数据表,把token存到数据库里,我现在追求简单可用,采用第二种方式,直接存到内存里。下面配置一个管理token的service:

    <beans:bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/>
    <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:bean>

    下面配置4个基本的bean:分别处理访问成功、访问拒绝、认证点和访问控制:

    <beans:bean id="oauth2AuthenticationEntryPoint"
                    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"/>
        
    <beans:bean id="oauth2AccessDeniedHandler"
                    class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>
    <beans:bean id="oauthUserApprovalHandler" 
                    class="org.springframework.security.oauth2.provider.approval.DefaultUserApprovalHandler"/>
        
    <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的server所能支持的请求类型:

    <oauth2:authorization-server client-details-service-ref="clientDetailsService" token-services-ref="tokenServices"
                                    user-approval-handler-ref="oauthUserApprovalHandler" >  
        <oauth2:authorization-code />
        <oauth2:implicit />
        <oauth2:refresh-token />
        <oauth2:client-credentials />
        <oauth2:password />
    </oauth2:authorization-server>

    比如说,如果配置本服务器不支持刷新token,那么就:

    <oauth2:refresh-token disabled="true" />

    我们的请求里,要把验证类型、用户名密码都作为表单参数提交,这就需要配置下面的filter:

    <beans:bean id="clientCredentialsTokenEndpointFilter"
                    class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <beans:property name="authenticationManager" ref="oauth2AuthenticationManager"/>
    </beans:bean>

    下面定义一种资源,指定spring要保护的资源,如果没有这个,访问控制的时候会说没有Authentication object:

    <oauth2:resource-server id="mobileResourceServer"
            resource-id="mobile-resource" token-services-ref="tokenServices" />

    好了,到此为止基本配置就都有了,下面就看访问控制的配置:在前面的拦截链上,已经为登录验证配了一个/auth/token,在这个标签下面添加对/json和/admin这两个路径的控制(这里没有再写这两个访问的controller,依旧用前面几篇文章中写好的):

    <http pattern="/json**" create-session="never"
            entry-point-ref="oauth2AuthenticationEntryPoint"
            access-decision-manager-ref="oauth2AccessDecisionManager">
        <anonymous enabled="false" />
        <intercept-url pattern="/json**" access="ROLE_USER" />
        <custom-filter ref="mobileResourceServer" before="PRE_AUTH_FILTER" />
        <access-denied-handler ref="oauth2AccessDeniedHandler" />
    </http>
    <http pattern="/admin**" create-session="never"
            entry-point-ref="oauth2AuthenticationEntryPoint"
            access-decision-manager-ref="oauth2AccessDecisionManager">
        <anonymous enabled="false" />
        <intercept-url pattern="/admin**" access="ROLE_ADMIN,SCOPE_READ" />
        <custom-filter ref="mobileResourceServer" before="PRE_AUTH_FILTER" />
        <access-denied-handler ref="oauth2AccessDeniedHandler" />
    </http>

    我们用oauth2AccessDecisionManager来做决策,这个地方需要注意,spring-security里面配置access="ROLE_USER,ROLE_ADMIN"是说user和admin都可以访问,是一个“或”的关系,但是这里是“与”的关系,比如第二个,需要ROLE_ADMIN并且当前的scope包含read才可以,否则就没有权限。认证失败会返回一段xml,这个可以自定义handler来修改,暂且按下不表。

    源码下载

    ps:使用方法:用户名密码前面spring-security中已经创建了用户,验证身份时访问:

    http://localhost:8080/demo4ssh-security-oauth2/oauth/token?client_id=mobile_1&client_secret=secret_1&grant_type=password&username=zhangsan&password=123456

    这时候会返回一个access_token:

    {"access_token":"4219a91f-45d5-4a07-9e8e-3acbadd0c23e","token_type":"bearer","refresh_token":"d41df9fd-3d36-4a20-b0b7-1a1883c7439d","expires_in":43199,"scope":"read write trust"}

    这之后再拿着这个access_token去访问资源:

    http://localhost:8080/demo4ssh-security-oauth2/admin?access_token=4219a91f-45d5-4a07-9e8e-3acbadd0c23e
  • 相关阅读:
    python 基础(三) list 的用法
    python 基础(二) string 的用法
    python 基础(四)购物车(list类型练习)
    python 基础(一)猜年龄游戏
    centOS 7 环境搭建之安装 python 3
    centOS 7 环境搭建之安装 JDK 8
    源代码管理相关命令(Git常用命令、Nuget常用命令、CMD常用命令)
    .Net Core 3.0 关于Windows Form和WPF的全面支持
    .Net Core 常用开发工具(IDE和运行时、Visual Studio插件、Visual Studio Code插件)
    .Net Core 精选公众号集合(保持更新)
  • 原文地址:https://www.cnblogs.com/smarterplanet/p/4088479.html
Copyright © 2020-2023  润新知