spring-security(2)

记录一下spring security的配置

配置详解

<?xml version="1.0" encoding="UTF-8"?>
<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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 指定登录页面不拦截 -->
    <http security="none" pattern="/login.htm"/>

    <http auto-config="true">
        <!--
            login-page  指定登录页面
            username-parameter  登录的用户名,默认是“j_username”
            password-parameter  登录的密码,默认是“j_password”
            login-processing-url  登录提交的页面,默认是“/j-spring-security-check”
            default-target-url  指定登录成功后跳转的界面
            authentication-success-handler-ref  指定登录成功后调用的服务,这里指定后default-target-url就不生效, AuthenticationSuccessHandler
            authentication-failure-url 指定登录失败后跳转的界面
            authentication-failure-handler-ref="authenticationFailHandler" 指定登录失败后调用的服务,这里指定后authentication-failure-url就不生效, SimpleUrlAuthenticationFailureHandler
        -->
        <form-login
                login-page="/login.htm"
                username-parameter="username"
                password-parameter="password"
                login-processing-url="/spring-security-check"
                default-target-url="/user/welcome.htm"
                authentication-success-handler-ref="authenticationSuccessHandler"
                authentication-failure-url="/user/fail.htm"
                authentication-failure-handler-ref="authenticationFailHandler"
        />

        <!--禁用CSRF保护功能 默认开启-->
        <csrf disabled="true"/>

        <!--
        退出登录配置
            logout-url:退出登录提交的页面,默认j_spring_security_logout
            success-handler-ref: 成功退出登录的事件 LogoutSuccessHandler
        -->
        <logout logout-url="/spring_security_logout" success-handler-ref="logoutSuccessHandler" />

        <!--intercept-url定义了一个权限控制的规则。
                pattern:进行权限控制的url
                access:需要什么权限,以逗号分隔的角色列表,只需拥有其中的一个角色就能成功访问
            -->
        <intercept-url pattern="/" access="hasRole('role1')" />
        <intercept-url pattern="/*.htm" access="hasRole('role1')"/>
        <intercept-url pattern="/**/.htm" access="hasRole('role1')"/>
    </http>

    <!--开启权限注解支持 -->
    <global-method-security secured-annotations="enabled" jsr250-annotations="enabled"/>

    <beans:bean id="userService" class="com.yitop.feng.service.UserService"/>

    <beans:bean id="authenticationSuccessHandler" class="com.yitop.feng.service.security.AuthenticationSuccessHandlerImpl" />

    <beans:bean id="authenticationFailHandler" class="com.yitop.feng.service.security.AuthenticationFailHandlerImpl" />

    <!--
    authentication-manager元素指定了一个AuthenticationManager,其需要一个AuthenticationProvider来进行真正的认证,
    默认情况下authentication-provider对应一个UserDetailsService来获取用户信息(即查询数据库)。
    -->
    <authentication-manager>
        <authentication-provider user-service-ref="userService">
            <!--
            hash    指定密码加密方式 plaintext sha sha-256 md4 md5 {sha} {ssha}
                    加密算法  PasswordEncoder  实现类 plaintext PlaintextPasswordEncoder
                    sha                          ShaPasswordEncoder
                    sha-256                  ShaPasswordEncoder,使用时new ShaPasswordEncoder(256)
                    md4                         Md4PasswordEncoder
                    md5                         Md5PasswordEncoder
                    {sha}                       LdapShaPasswordEncoder
                    {ssha}                     LdapShaPasswordEncoder

            base64  表示是否需要对加密后的密码使用BASE64进行编码,默认是false
            -->
            <password-encoder hash="md5" base64="true"/>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

url权限控制表达式

hasRole([role])                 当前用户是否拥有指定角色。
hasAnyRole([role1,role2])       多个角色是一个以逗号进行分隔的字符串。如果当前用户拥有指定角色中的任意一个则返回true。
hasAuthority([auth])            等同于hasRole
hasAnyAuthority([auth1,auth2])  等同于hasAnyRole
Principle                       代表当前用户的principle对象
authentication                  直接从SecurityContext获取的当前Authentication对象
permitAll                       允许所有
denyAll                         拒绝所有

注解功能

<!-- 开启注解功能 -->
<security:global-method-security jsr250-annotations="enabled"/>

@RolesAllowed({"ROLE_USER", "ROLE_ADMIN"})
public User find(int id) {
  System.out.println("find user by id............." + id);
  return null;
}

//允许ROLE_USER或ROLE_ADMIN使用find方法

//@PermitAll 允许所有  
//@DenyAll  拒绝所有
posted @ 2017-03-28 15:35  fengzp  阅读(404)  评论(0编辑  收藏  举报