云中烛火

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

spring  shiro的过滤器配置

<!-- 基于Form表单的身份验证过滤器 -->
    <bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
        <property name="usernameParam" value="username"/>
        <property name="passwordParam" value="password"/>
        <property name="rememberMeParam" value="rememberMe"/>
        <property name="loginUrl" value="/login"/>
    </bean>

 

<!-- Shiro的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login"/>
        <property name="filters">
            <util:map>
                <entry key="authc" value-ref="formAuthenticationFilter"/>
                <entry key="sysUser" value-ref="sysUserFilter"/>
                <!--添加ssl支持-->
                <!--<entry key="ssl" value-ref="sslFilter"/>-->
                <!-- 结束ssl支持-->
            </util:map>
        </property>
        <property name="filterChainDefinitions">
            <value>
                /login=authc
                /logout=logout
                /authenticated=authc
                /**=user,sysUser
            </value>
        </property>
    </bean>

 

 

 

 

登陆的action

@RequestMapping(value = "/login"    )
    public String showLoginForm(HttpServletRequest req, Model model) {
        String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误:" + exceptionClassName;
        }
        model.addAttribute("error", error);
        return "login";
    }

 

 

登陆表单页面

<html>
<head>
    <title>登录</title>
    <style>.error{color:red;}</style>
</head>
<body>

<div class="error">${error}</div>
<form action="" method="post">
    用户名:<input type="text" name="username" value="<shiro:principal/>"><br/>
    密码:<input type="password" name="password"><br/>
    自动登录:<input type="checkbox" name="rememberMe" value="true"><br/>
    <input type="submit" value="登录">
</form>

</body>
</html>

 

 

两个情况说明:

1.  在浏览器中输入地址:  localhost:8080/  的流程

   配置文件中/**=user,sysUser,所以先走  UserFilter 这个过滤器

   此时用户未登陆,在UserFilter 过滤器中,会重定向到  /login,进入登陆页面,并且记住此时的请求地址  "/"

 在页面中输入用户名密码,此时action=""是这样的,当点击提交按钮时,表单会提交到 当前的地址 "/login",post方法而且带着用户名和密码

   /login=authc 因为这个配置,会经过FormAuthenticationFilter这个过滤器,该类会判断此请求是否为登陆请求,如果是,则走登陆流程(subject.login(xxx)),登陆成功后, 会重现跳转到 上次保存的请求地址 ”/"

 

2. 在浏览器中输入地址:localhost:8080/login

/login=authc 因为这个配置,会经过FormAuthenticationFilter这个过滤器,该类会判断此请求是否为登陆请求,如果是,则走登陆流程(subject.login(xxx)),登陆成功后, 因为没有其他的求情,成功后会直接跳转到successUrl;如果没有配置,默认是”/"

 

 

 

 看一下FormAuthenticationFilter的继承关系

重要的方法

 public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        return isAccessAllowed(request, response, mappedValue) || onAccessDenied(request, response, mappedValue);
    }

先判断是否已经授权,如果没有再判断是否是登陆请求

 

 

 

 

shiro的认证流程


 

posted on 2017-01-06 17:23  云中烛火  阅读(71)  评论(0)    收藏  举报