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

序:

  在上一篇中,咱们已经对于项目已经做了基本的配置,这一篇文章开始学习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>

大致解释:
<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

package org.shiro.demo.service.realm;

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;
}
}
}

继承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";
}
}
}

四、新建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>

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

摘自:http://www.cnblogs.com/xql4j/archive/2013/03/30/2990920.html

上一篇:React-Native学习手册----搭建基于ios平台的开发环境


下一篇:springboot后台运行