package com.shiro.realm;
import java.sql.Connection;
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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import com.shiro.dao.UserDao;
import com.shiro.entity.User;
import com.shiro.utils.ConnectionUtil;
/**
* 自定义realm
* 步骤:
* s1:继承 AuthorizingRealm 并实现其方法
* s2:验证当前用户
* s3:给当前登录成功的用户授予角色和权限
* s4:
* s5:
* @author ITDragon
*
*/
public class MyRealm extends AuthorizingRealm{
private UserDao userDao = new UserDao();
// 给当前登录成功的用户授予角色和权限
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
// 获取用户名
String userName = (String) principals.getPrimaryPrincipal();
// 认证信息
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
Connection connection = null;
try{
connection = ConnectionUtil.getConnection();
// 设置角色
simpleAuthorizationInfo.setRoles(userDao.getUserRoles(connection , userName));
// 设置权限
simpleAuthorizationInfo.setStringPermissions(userDao.getUserPermissions(connection , userName));
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ConnectionUtil.closeConnection(connection);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
// 验证当前用户
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
// 获取用户信息
String userName = (String) token.getPrincipal();
Connection connection = null;
try{
connection = ConnectionUtil.getConnection();
// 通过用户名找到用户
User user = userDao.getByUserName(connection, userName);
if(null != user){
// 三个参数分别是: 用户名 , 密码 , rememberMe
AuthenticationInfo authcInfo =
new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(),"false");
return authcInfo;
}else{
return null;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ConnectionUtil.closeConnection(connection);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
shiro.realm.rar (34个子文件)
shiro.web
pom.xml 1KB
db_shiro.sql 2KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
shiro-web
shiro.web
pom.xml 1KB
pom.properties 223B
MANIFEST.MF 114B
classes
log4j.properties 456B
com
shiro
dao
UserDao.class 3KB
realm
MyRealm.class 3KB
servlet
LoginServlet.class 3KB
entity
User.class 974B
utils
ConnectionUtil.class 2KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.common.component 757B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 575B
org.eclipse.jdt.core.prefs 430B
src
test
resources
java
main
webapp
unauthorized.jsp 295B
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 714B
shiro.ini 469B
lib
login.jsp 462B
success.jsp 679B
resources
log4j.properties 456B
java
com
shiro
dao
UserDao.java 2KB
realm
MyRealm.java 3KB
servlet
LoginServlet.java 2KB
entity
User.java 513B
utils
ConnectionUtil.java 2KB
.project 1KB
.classpath 2KB
共 34 条
- 1
资源评论
- 随心村2016-03-28谢谢分享,东西很好
ITDragon龙
- 粉丝: 800
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功