【shiro权限管理】7.简单shiro认证实现
现在在第三章的例子基础上,对代码做一些调整来实现简单的web项目认证
首先加入登录页面login.jsp、登录成功页面list.jsp
//登录页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录</title> </head> <body> <form action="userAuth/login" method="post"> 账号:<input type="text" name="username"><br/><br/> 密码:<input type="password" name="password"><br/><br/> <input type="submit" value="登录"> </form> </body> </html> //登录成功页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>首页</title> </head> <body> 登录成功!欢迎访问首页O(∩_∩)O <a href="userAuth/logout">登出</a> </body> </html>
以及登录页面输入用户名密码之后访问的认证控制器
@Controller @RequestMapping("userAuth") public class ShiroLoginController { @RequestMapping("login") public String login(String username,String password){ //获取当前的Subject Subject currentUser = SecurityUtils.getSubject(); //测试当前用户是否已经被认证(即是否已经登录) if (!currentUser.isAuthenticated()) { //将用户名与密码封装为UsernamePasswordToken对象 UsernamePasswordToken token = new UsernamePasswordToken(username, password); token.setRememberMe(true);//记录用户 try { currentUser.login(token);//调用Subject的login方法执行登录 } catch (AuthenticationException e) {//所有认证时异常的父类 System.out.println("登录失败:"+e.getMessage()); } } //跳转到登录成功页面list return "redirect:/list.jsp"; } }
然后在applicationContext.xml中shiroFilter对应url过滤机制中添加对登录、登出的放行
<property name="filterChainDefinitions"> <value> /login.jsp = anon #登录页面点击登录后访问的这个地址必须给放行 /userAuth/login = anon #这行表示访问这个地址就做出登出动作 无需自定义方法 shiro的logout拦截器会实现登出功能 /userAuth/logout = logout # everything else requires authentication: /** = authc </value> </property>
上一篇提到Shiro认证时需要一个Realm,我们已经在applicationContext.xml中配置了ShiroRealm,另外现在只运用到了Shiro的认证功能,因此我们可以让ShiroRealm继承AuthenticatingRealm,并重写它的doGetAuthenticationInfo方法。另外在本例中用户信息暂时用Map来存储,实际生产中一般从数据库中获取,一个意思
public class ShiroRealm extends AuthenticatingRealm{ private static Map<String,User> userMap = new HashMap<String,User>(); static{ //使用Map模拟数据库获取User表信息 userMap.put("jack", new User("jack","123",false)); userMap.put("tom", new User("tom","123",false)); userMap.put("jean", new User("jean","123",true)); } @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token) throws AuthenticationException { //1.把AuthenticationToken转换为UsernamePasswordToken UsernamePasswordToken userToken = (UsernamePasswordToken) token; //2.从UsernamePasswordToken中获取username String username = userToken.getUsername(); //Map模拟数据库取数据 User u = userMap.get(username); //4.若用户不行存在,可以抛出UnknownAccountException if(u==null){ throw new UnknownAccountException("用户不存在"); } //5.若用户被锁定,可以抛出LockedAccountException if(u.isLocked()){ throw new LockedAccountException("用户被锁定"); } //6.根据用户的情况,来构建AuthenticationInfo对象,通常使用的实现类为SimpleAuthenticationInfo //以下信息是从数据库中获取的 //1)principal:认证的实体信息,可以是username,也可以是数据库表对应的用户的实体对象 Object principal = u.getUsername(); //2)credentials:密码 Object credentials = u.getPassword(); //3)realmName:当前realm对象的name,调用父类的getName()方法即可 String realmName = getName(); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal,credentials,realmName); return info; } }
其中User类
public class User { private String username; private String password; private boolean locked; public User(String username, String password, boolean locked) { super(); this.username = username; this.password = password; this.locked = locked; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isLocked() { return locked; } public void setLocked(boolean locked) { this.locked = locked; } }
至此我们的一个简单的Shiro校验工程编写完毕。可以运行项目做一些简单的测试
注意我们使用的AuthenticationInfo对象为SimpleAuthenticationInfo实现类,而一般密码都是会进行加密的,所以当加密的时候要使用其它类型的AuthenticationInfo实现类,这个在下一篇总结中会提到。
相关推荐
杜鲁门 2020-11-05
luckyxl0 2020-08-16
Dullonjiang 2020-08-09
xclxcl 2020-08-03
zmzmmf 2020-08-03
MicroBoy 2020-08-02
ganjing 2020-08-02
likesyour 2020-08-01
zmzmmf 2020-07-09
MicroBoy 2020-07-05
zzhao 2020-06-26
子云 2020-06-18
visionzheng 2020-06-07
neweastsun 2020-06-04
ErixHao 2020-06-03
GDreams0 2020-06-01
ganjing 2020-05-29
zmzmmf 2020-05-28
nullcy 2020-05-26