package cn.ssms.realm;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ssms.model.User;
import cn.ssms.service.UserService;
import cn.ssms.util.CipherUtil;
import cn.ssms.util.EncryptUtils;
public class ShiroDbRealm extends AuthorizingRealm {
private static Logger logger = LoggerFactory.getLogger(ShiroDbRealm.class);
private static final String ALGORITHM = "MD5";
@Autowired
private UserService userService;
public ShiroDbRealm() {
super();
}
/**
* 认证回调函数, 登录时调用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
System.out.println(token.getUsername());
User user = userService.findUserByLoginName(token.getUsername());
System.out.println(user);
if (user != null) {
return new SimpleAuthenticationInfo(user.getName(), user.getPassword(), getName());
}else{
throw new AuthenticationException();
}
}
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
/* 这里编写授权代码 */
Set<String> roleNames = new HashSet<String>();
Set<String> permissions = new HashSet<String>();
roleNames.add("admin");
roleNames.add("zhangsan");
permissions.add("user.do?myjsp");
permissions.add("login.do?main");
permissions.add("login.do?logout");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
info.setStringPermissions(permissions);
return info;
}
/**
* 更新用户授权信息缓存.
*/
public void clearCachedAuthorizationInfo(String principal) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
clearCachedAuthorizationInfo(principals);
}
/**
* 清除所有用户授权信息缓存.
*/
public void clearAllCachedAuthorizationInfo() {
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
if (cache != null) {
for (Object key : cache.keys()) {
cache.remove(key);
}
}
}
// @PostConstruct
// public void initCredentialsMatcher() {//MD5加密
// HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(ALGORITHM);
// setCredentialsMatcher(matcher);
// }
}
没有合适的资源?快使用搜索试试~ 我知道了~
SSM(spring+springmvc+mybatis)+shiro 实现安全登录的demo 实例源码
共83个文件
jar:30个
xml:12个
java:10个
4星 · 超过85%的资源 需积分: 0 313 下载量 159 浏览量
2017-08-23
10:29:43
上传
评论 41
收藏 11.22MB ZIP 举报
温馨提示
SSM(spring+springmvc+mybatis)+shiro 实现安全登录的demo SSM(spring+springmvc+mybatis)+shiro 实现安全登录的demo
资源推荐
资源详情
资源评论
收起资源包目录
shirodemo.zip (83个子文件)
shirodemo
src
cn
ssms
dao
UserMapper.java 388B
realm
ShiroDbRealm.java 3KB
controller
UserControler.java 3KB
model
Role.java 591B
User.java 1KB
UserRole.java 571B
util
CipherUtil.java 2KB
EncryptUtils.java 277B
service
UserServiceImpl.java 587B
UserService.java 182B
mapping
UserMapper.xml 3KB
resource
spring-shiro.xml 2KB
config.properties 1KB
spring-mybatis.xml 5KB
spring-mvc.xml 2KB
spring.xml 729B
log4j.properties 2KB
.classpath 3KB
build
classes
spring-shiro.xml 2KB
config.properties 1KB
spring-mybatis.xml 5KB
spring-mvc.xml 2KB
cn
ssms
dao
UserMapper.class 449B
realm
ShiroDbRealm.class 4KB
controller
UserControler.class 4KB
model
UserRole.class 900B
User.class 2KB
Role.class 1KB
util
CipherUtil.class 3KB
EncryptUtils.class 668B
service
UserService.class 240B
UserServiceImpl.class 1KB
mapping
UserMapper.xml 3KB
spring.xml 729B
log4j.properties 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.core.resources.prefs 88B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 560B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 629B
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
views
showUser.jsp 384B
index.jsp 3KB
login.jsp 784B
NewFile.jsp 516B
error
401.jsp 857B
noperms.jsp 648B
404.jsp 643B
include
common.jsp 294B
lib
spring-tx-3.1.1.RELEASE.jar 241KB
aspectjweaver-1.7.1.jar 1.71MB
mysql-connector-java-5.1.21.jar 809KB
commons-logging-1.1.1.jar 59KB
spring-web-3.2.0.RELEASE.jar 610KB
spring-test-3.2.0.RELEASE.jar 441KB
jackson-core-asl-1.9.11.jar 227KB
spring-beans-3.2.0.RELEASE.jar 591KB
fastjson-1.1.26.jar 348KB
shiro-ehcache-1.2.2.jar 15KB
aopalliance-1.0.jar 4KB
spring-webmvc-3.2.0.RELEASE.jar 621KB
commons-beanutils-1.8.3.jar 227KB
servlet-api-3.0-alpha-1.jar 170KB
commons-fileupload-1.2.2.jar 58KB
spring-jdbc-3.1.1.RELEASE.jar 395KB
jackson-mapper-asl-1.9.11.jar 762KB
mybatis-spring-1.1.1.jar 40KB
spring-aop-3.2.0.RELEASE.jar 327KB
spring-expression-3.2.0.RELEASE.jar 189KB
spring-core-3.2.0.RELEASE.jar 843KB
log4j-1.2.17.jar 478KB
shiro-spring-1.2.2.jar 24KB
spring-context-3.2.0.RELEASE.jar 834KB
shiro-core-1.2.2.jar 358KB
shiro-web-1.2.2.jar 139KB
druid-0.2.9.jar 1.4MB
mybatis-3.1.1.jar 636KB
slf4j-log4j12-1.6.2.jar 10KB
slf4j-api-1.6.4.jar 25KB
web.xml 3KB
.project 1KB
共 83 条
- 1
Silence-wen
- 粉丝: 111
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页