spring集成shiro
文章目录
关于spring集成shiro的配置在官网上有详细的,在w3c上也有常见的 Spring 配置,我们从简单的开始讲起,后面再讲加密、缓存、session、cookie、注解等等
全部的代码在我的Github中,只要打成war包,放到tomcat里面跑就行
环境搭建
整体的框架结构是这样的
引入下面的maven依赖
<!-- Spring MVC 依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- Shiro 依赖包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.4.0</version>
</dependency>
<!-- Log4j 日志依赖包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- SLF4J 与 Log4j 适配包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
代码
shiro的简单权限管理
实体类 User.java
public class User {
private String username;
private String password;
private boolean rememberMe;//用于cookie的是否记住
//省略构造函数和get,set
}
自定义一个Realm
/**
* 自定义的 Realm
* author:ligz
*/
public class MyRealm extends AuthorizingRealm {
private static final Logger logger = Logger.getLogger(MyRealm.class);
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
logger.info("从数据库中读取授权信息...");
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
Set<String> roles = new HashSet<>();
roles.add("admin");
authorizationInfo.setRoles(roles);
Set<String> permissions = new HashSet<>();
permissions.add("add");
authorizationInfo.setStringPermissions(permissions);
return authorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String username = (String) authenticationToken.getPrincipal();
User user = selectUserByUserName(username);
if (user == null) {
throw new UnknownAccountException("账户不存在");
}
return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes("TestSalt"), super.getName());
}
/**
* 仿照数据库信息
* @param username
* @return
*/
private User selectUserByUserName(String username) {
if ("ligz".equals(username)) {
//return new User(username, "123456");
return new User(username, "e5f728a966d050296c428290c9160dda");//这个是123456加上TestSalt盐后的值
}
return null;
}
}
这是我们重点讲解的地方,在这里我们部访问数据库,就用一个假的账户好了。username是ligz
,password是123456
在我们前面的blog里面我们都是用的手工new出来的DefaultSecurityManager
,我们这里使用spring的容器帮助我们管理
<!-- shiro 过滤器, 要与 web.xml 中的 Filter Name 相同-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 登录页面, 未认证时访问需要认证或授权的资源会自动跳转到此页面 -->
<property name="loginUrl" value="/login.jsp"/>
<!-- 登录成功页面 -->
<property name="successUrl" value="/index.jsp"/>
<!-- 登录后, 访问未授权的资源会跳转到此页面 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
/login = anon
/user.jsp = roles[user]