• SpringMVC+Apache Shiro+JPA(hibernate)案例教学(二)基于SpringMVC+Shiro的用户登录权限验证


    一、Shiro配置的简要说明。

      有心人可能注意到了,在上一章的applicationContext.xml配置文件中,包含以下配置。

    <!-- 項目自定义的Realm -->
        <bean id="shiroDbRealm" class="org.shiro.demo.service.realm.ShiroDbRealm" ></bean>
        
        <!-- Shiro Filter -->
        <bean id="shiroFilter"
            class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager" />
            <property name="loginUrl" value="/" />
            <property name="successUrl" value="/system/main" />
            <property name="unauthorizedUrl" value="/system/error" />
            <property name="filterChainDefinitions">
                <value>
                /login = anon
                /validateCode = anon
                /** = authc
                </value>
            </property>
        </bean>
    View Code

    大致解释: <bean id="shiroDbRealm" class="org.shiro.demo.service.realm.ShiroDbRealm" ></bean> 这个就是指定Shiro验证用户登录的类为自定义的ShiroDbRealm.java。

    Shiro Filter当中: securityManager:这个属性是必须的。 loginUrl :没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面 successUrl :登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。 unauthorizedUrl :没有权限默认跳转的页面。 filterChainDefinitions : 就是需要验证的地址的列表,常用的包含anon、authc、perms、roles、user、logout。            /login = anon  代表后缀为/login的链接不验证            /** = authc   代表其它后缀的链接都进行登录验证,需登录后才能访问。

    二、新建ShiroDbRealm类

    ShiroDbRealm.java

    import javax.annotation.Resource;
    
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.shiro.demo.entity.User;
    import org.shiro.demo.service.IUserService;
    
    public class ShiroDbRealm extends AuthorizingRealm{
    
        @Resource(name="userService")
        private IUserService userService;
    
        protected AuthorizationInfo doGetAuthorizationInfo(
                PrincipalCollection principals) {
            return null;
        }
    
        /**
         *  认证回调函数,登录时调用.
         */
        protected AuthenticationInfo doGetAuthenticationInfo(
                AuthenticationToken authcToken) throws AuthenticationException {
            UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
            User user = userService.getByAccount(token.getUsername());
            if (user != null) {
                return new SimpleAuthenticationInfo(user.getAccount(), user
                        .getPassword(), user.getNickname());
            } else {
                return null;
            }
        }
    }
    View Code

    继承AuthorizingRealm类,且重写doGetAuthorizationInfo及doGetAuthenticationInfo方法。 doGetAuthorizationInfo : 验证当前Subject(可理解为当前用户)所拥有的权限,且给其授权。在下一章说明。 doGetAuthenticationInfo : 验证当前Subject登录。 userService.getByAccount(token.getUsername());是自定义的方法。 (解释这行的原因是当初在网上看demo的时候,实在没搞清那些代码中这一行的意义,后来突然顿悟,请原谅小生愚钝。)

    三、新建UserController.java类

    @Controller
    public class UserController {
    private static final Log log = LogFactory.getLog(UserController.class);
        
        /**
         * 判断用户是否登录
         * @param currUser
         * @return
         */
        @RequestMapping(value = "/login",method=RequestMethod.POST)
        public String isLogin(User currUser){
            Subject user = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(currUser.getAccount(),currUser.getPassword());
            token.setRememberMe(true);
            try {
                user.login(token);
                return "redirect:/main";
            }catch (AuthenticationException e) {
                log.error("登录失败错误信息:"+e);
                token.clear();
                return "redirect:/login";
            }
        }
    }
    View Code

    四、新建login.jsp

    <form action="<%=basePath%>/login" method="post">
    用户名:<input type="text" name="account"/> <br/>
    密码:<input type="text" name="password"/><br/>
    <input type="submit" value="登录" />
    </form>
    View Code

    然后通过SpringMVC访问到login.jsp页面,测试Shiro的用户验证。

  • 相关阅读:
    Spring Richclient — 企业级富客户端开发框架介绍,第 1 部分
    oracle的表名、字段名、constraint名的长度限制分别是多少?
    eclipse指定启动的jdk
    女人为什么爱攀比?
    Oracle一些常用的查询命令总结(持续更新)
    js冒泡事件之之之
    Aspose插件
    oracle11g导入到10g
    转:OpenCms 9.0.1汉化
    metasploit常用命令
  • 原文地址:https://www.cnblogs.com/lixj/p/3403601.html
Copyright © 2020-2023  润新知