Apache Shiro是一个安全框架,对于身份验证还有权限管理比较简单(一点都不简单),尤其是在看了网上很多博客教程,各种坑,真不知那些人怎么想的,乱写代码就乱发。
本篇文章用的环境如下:
1.Intellij IDEA 2017.1
2.JDK1.8
3.Maven3.0
这里是Shiro的是官方的地址(貌似是吧)https://github.com/apache/shiro
这里是我觉得写的最用心最不错的教程:http://blog.csdn.net/Angel_G/article/category/6655167
从我给的第一个官方的Shiro网址里我截取了一部分的配置文件,可以看出,Shiro相关的有用户名,密码,权限,还有许可。
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
接下来就是在SSM基础上添加Shiro框架。
步骤1:
在Pom.xml添加依赖:
org.apache.shiro
shiro-spring
1.2.2
在已经搭建完的SSM框架上,我们创建数据库还有表
create database shiro;
use shiro;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
然后写dao层,service层,web层(很简单就是查一条数据而已)。
步骤2:
整合Shiro安全框架
首先在web.xml添加Filter
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
/*
然后再spring-web.xml添加配置
/admin/alist*=authc,perms[admin:manage]
/user/ulist*=authc,perms[user:manage]
接下来写我们验证信息的Realm
myrealm
public class myrealm extends AuthorizingRealm {
@Autowired
private UserService userService;
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String currentUsername = (String)super.getAvailablePrincipal(principalCollection);
System.out.println("-----------------------doGetAuthorizationInfo----------------------");
System.out.println("当前名字:"+currentUsername);
SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();
simpleAuthorInfo.addRole("admin");
//添加权限
simpleAuthorInfo.addStringPermission("admin:manage");
return simpleAuthorInfo;
}
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
System.out.print("验证当前Subject时获取到token:");
System.out.println(ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE));
User user = userService.selectUserByNameService(token.getUsername());
String password = new String((char[])token.getCredentials());
System.out.println("--------------密码是:------"+password);
if (user.getPassword().equals(password)) {
AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), this.getName());
this.setAuthenticationSession(user.getUsername());
return authcInfo;
}
return null;
}
private void setAuthenticationSession(Object value) {
Subject currentUser = SecurityUtils.getSubject();
if (null != currentUser) {
Session session = currentUser.getSession();
System.out.println("当前Session超时时间为[" + session.getTimeout() + "]毫秒");
session.setTimeout(1000 * 60 * 60 * 2);
System.out.println("修改Session超时时间为[" + session.getTimeout() + "]毫秒");
session.setAttribute("currentUser", value);
}
}
}
至此整合搭建完毕!
测试一:
输入错误的用户名或者密码
结果登陆不了
测试二:
输入正确的账号密码:
登陆成功!
由于在MyRealm中我们给用户露娜添加了
/ /添加权限
simpleAuthorInfo.addStringPermission("admin:manage");
//然后我们在spring-web.xml中添加资源的访问是这样子的
/admin/alist*=authc,perms[admin:manage]
/user/ulist*=authc,perms[user:manage]
也就是说明我们的用户露娜所拥有的权限是(admin:manage),他可以访问/admin/alist,而不能访问user/ulist。
测试三:
结果是用户露娜真的可以访问admin,不能访问user/ulist。权限控制启到作用。
这个项目下载地址https://github.com/Elricyo/ShiroDemo
喜欢可以点个Star,谢谢哈!