package com.xxx.boot.shiro;
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.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import com.xxx.boot.login.entity.Admin;
import com.xxx.boot.login.service.AdminService;
public class MyRealm extends AuthorizingRealm {
@Autowired
private AdminService adminService;
String pass;
/**
* 授权:
*
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Object principal = principalCollection.getPrimaryPrincipal();//获取登录的用户名
if("admin".equals(principal)){ //两个if根据判断赋予登录用户权限
info.addRole("admin");
}
if("user".equals(principal)){
info.addRole("list");
}
info.addRole("user");
return info;
}
/*
* 用户验证
*
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//1. token 中获取登录的 username! 注意不需要获取password.
Object principal = token.getPrincipal();
//2. 利用 username 查询数据库得到用户的信息.
Admin user=adminService.getAdminByName((String) principal);
if(user!=null){
pass=user.getPassword();
}
String credentials = pass;
//3.设置盐值 ,(加密的调料,让加密出来的东西更具安全性,一般是通过数据库查询出来的。 简单的说,就是把密码根据特定的东西而进行动态加密,如果别人不知道你的盐值,就解不出你的密码)
String source = "abcdefg";
ByteSource credentialsSalt = new Md5Hash(source);
//当前 Realm 的name
String realmName = getName();
//返回值实例化
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
return info;
}
//init-method 配置.
public void setCredentialMatcher(){
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
credentialsMatcher.setHashAlgorithmName("MD5");//MD5算法加密
credentialsMatcher.setHashIterations(1024);//1024次循环加密
setCredentialsMatcher(credentialsMatcher);
}
//用来测试的算出密码password盐值加密后的结果,下面方法用于新增用户添加到数据库操作的,我这里就直接用main获得,直接数据库添加了,省时间
public static void main(String[] args) {
String saltSource = "abcdef";
String hashAlgorithmName = "MD5";
String credentials = "123123";
Object salt = new Md5Hash(saltSource);
int hashIterations = 1024;
Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
System.out.println(result);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot模块化框架
共83个文件
xml:21个
prefs:17个
class:12个
需积分: 28 18 下载量 194 浏览量
2020-04-21
11:09:06
上传
评论
收藏 72KB ZIP 举报
温馨提示
最近自己学习的springboot模块化框架,供大家一起学习,也有不足的地方,但也可供学习的地方,大家一起学习
资源推荐
资源详情
资源评论
收起资源包目录
xxxboot.zip (83个子文件)
xxxboot
xxxboot-common
pom.xml 466B
target
test-classes
classes
META-INF
maven
com.xxx.boot
xxxboot-common
pom.xml 466B
pom.properties 260B
MANIFEST.MF 343B
com
xxx
boot
common
utils
Constants.class 899B
NavTabAjaxBean.class 2KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 145B
org.springframework.ide.eclipse.prefs 65B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 356B
src
test
resources
java
main
resources
java
com
xxx
boot
common
utils
Constants.java 797B
NavTabAjaxBean.java 2KB
.project 1KB
.classpath 1KB
pom.xml 2KB
xxxboot-login
pom.xml 2KB
target
test-classes
classes
config
mybatis-config.xml 589B
META-INF
maven
com.xxx.boot
xxxboot-login
pom.xml 2KB
pom.properties 257B
MANIFEST.MF 341B
application.properties 347B
com
xxx
boot
login
dao
AdminMapper.class 613B
controller
LoginController.class 4KB
AdminController.class 2KB
service
AdminService.class 619B
impl
AdminServiceImpl.class 2KB
entity
Admin.class 3KB
Application.class 725B
mapping
AdminMapper.xml 6KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 356B
src
test
resources
java
main
resources
config
mybatis-config.xml 589B
application.properties 347B
mapping
AdminMapper.xml 6KB
java
com
xxx
boot
login
dao
AdminMapper.java 410B
Application.java 309B
controller
LoginController.java 3KB
AdminController.java 1KB
service
AdminService.java 414B
impl
AdminServiceImpl.java 1KB
entity
Admin.java 2KB
.project 1KB
.classpath 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 57B
.project 387B
xxxboot-db
pom.xml 787B
target
test-classes
classes
META-INF
maven
com.xxx.boot
xxxboot-db
pom.xml 787B
pom.properties 248B
MANIFEST.MF 335B
application.properties 468B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 145B
org.springframework.ide.eclipse.prefs 65B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 356B
src
test
resources
java
main
resources
application.properties 468B
java
.project 1KB
.classpath 1KB
xxxboot-shiro
pom.xml 868B
target
test-classes
classes
META-INF
maven
com.xxx.boot
xxxboot-shiro
pom.xml 868B
pom.properties 257B
MANIFEST.MF 341B
shiro
ehcache-shiro.xml 840B
xxx-shiro.xml 2KB
com
xxx
boot
shiro
ShiroRealm.class 3KB
ShiroFilter.class 2KB
MyRealm.class 4KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 145B
org.springframework.ide.eclipse.prefs 65B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 356B
src
test
resources
java
main
resources
shiro
ehcache-shiro.xml 840B
xxx-shiro.xml 2KB
java
com
xxx
boot
shiro
ShiroRealm.java 2KB
MyRealm.java 4KB
ShiroFilter.java 2KB
.project 1KB
.classpath 1KB
共 83 条
- 1
资源评论
slinvoyage
- 粉丝: 3
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功