《伸手系列》第一集-Shiro安全认证框架的从入门到“出门”

《伸手系列》第一集-Shiro安全认证框架的从入门到“出门”

1.Shiro简介

–author:写文章的需要,所以写简介里面的内容给完全的新手,大家心急的可以直接拉到第二点开始。

Apache Shiro是Java的一个安全框架。功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案。

实际上,Shiro的主要功能是管理应用程序中与安全相关的全部,同时尽可能支持多种实现方法。Shiro是建立在完善的接口驱动设计和面向对象原则之上的,支持各种自定义行为。Shiro提供的默认实现,使其能完成与其他安全框架同样的功能,这不也是我们一直努力想要得到的吗!
Apache Shiro相当简单,对比Spring Security,可能没有Spring Security做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所以使用小而简单的Shiro就足够了。对于它俩到底哪个好,这个不必纠结,能更简单的解决项目问题就好了。

Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。Shiro可以帮助我们完成:认证、授权、加密、会话管理、与Web集成、缓存等。这不就是我们想要的嘛,而且Shiro的API也是非常简单;其基本功能点如下图所示:

Authentication:身份认证/登录,验证用户是不是拥有相应的身份;
Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限;
Session Manager:会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通JavaSE环境的,也可以是如Web环境的;
Cryptography:加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;
Web Support:Web支持,可以非常容易的集成到Web环境;
Caching:缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可以提高效率;
Concurrency:shiro支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去;
Testing:提供测试支持;
Run As:允许一个用户假装为另一个用户(如果他们允许)的身份进行访问;
Remember Me:记住我,这个是非常常见的功能,即一次登录后,下次再来的话不用登录了。
记住一点,Shiro不会去维护用户、维护权限;这些需要我们自己去设计/提供;然后通过相应的接口注入给Shiro即可。

1.2 过滤器

过滤器简称

对应的java类 anon

org.apache.shiro.web.filter.authc.AnonymousFilter authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter port

org.apache.shiro.web.filter.authz.PortFilter rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter ssl

org.apache.shiro.web.filter.authz.SslFilter user

org.apache.shiro.web.filter.authc.UserFilter logout

org.apache.shiro.web.filter.authc.LogoutFilter

1.3 Jsp shiro标签

** **标签名称

标签条件(均是显示标签内容) shiro:authenticated

登录之后 shiro:notAuthenticated

不在登录状态时 shiro:guest

用户在没有RememberMe时 shiro:user

用户在RememberMe时 <shiro:hasAnyRoles name=*”abc,123” >*

在有abc或者123角色时 <shiro:hasRole name=*”abc”>*

拥有角色abc <shiro:lacksRole name=*”abc”>*

没有角色abc <shiro:hasPermission name=*”abc”>*

拥有权限资源abc <shiro:lacksPermission name=*”abc”>*

没有abc权限资源 shiro:principal

默认显示用户名称

1.4Spring security 与apache shiro 差别

shiro配置更加容易理解,容易上手;security配置相对比较难懂。
在spring的环境下,security整合性更好。Shiro对很多其他的框架兼容性更好,号称是无缝集成。
shiro 不仅仅可以使用在web中,它可以工作在任何应用环境中。
在集群会话时Shiro最重要的一个好处或许就是它的会话是独立于容器的。
Shiro提供的密码加密使用起来非常方便。

1.5 控制精度

Shiro也支持注解方式。
注解方式控制权限只能是在方法上控制,无法控制类级别访问。
过滤器方式控制是根据访问的URL进行控制。允许使用/*匹配URL,所以可以进行粗粒度,也可以进行细粒度控制。

2.实战演练

2.1 开发步骤

1、引入依赖pom.xml

1
2
3
4
5
6
<!-- Apache Shiro 权限架构 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.2.3</version>
</dependency>

2、核心filter,一个filter相当于10个filter;web.xml

注意:shiro的filter必须在struts2的filter之前,否则action无法创建

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Shiro Security filter -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3、在spring applicationContext.xml中记载shiro配置文件

1
<import resource="spring/applicationContext-shiro.xml"/>

和ehcache支持ehcache-shiro.xml

1
2
3
4
5
6
7
8
9
10
11
12
<ehcache updateCheck="false" name="shiroCache">

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>

4、applicationContext-shiro.xml,配置校验的策略,哪些校验,哪些放行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">

<description>Shiro</description>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="authRealm"/>
<!-- 二级缓存 -->
<property name="cacheManager" ref="shiroEhcacheManager"/>
</bean>

<!-- 自定义权限认证 -->
<bean id="authRealm" class="cn.itcast.jk.shiro.AuthRealm">
<property name="userService" ref="userService"/>
<!--property name="credentialsMatcher" ref="credentialsMatcher"/-->
</bean>

<!-- 设置密码加密策略 -->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"/>
</bean>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/index.jsp"></property>
<!-- 没有权限或者失败后跳转的页面 -->
<property name="successUrl" value="/home.action"></property>
<property name="filterChainDefinitions">
<!-- , roles[admin], perms[document:read]-->
<value>
/index.jsp* = anon
/home* = anon
/sysadmin/login/login.jsp* = anon
/sysadmin/login/logout.jsp* = anon
/login* = anon
/logout* = anon

/*.* = authc
/resource/** = anon
</value>
</property>
</bean>

<!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- 生成代理,通过代理进行控制 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>

<!-- 安全管理器 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>

</beans>

5、自定义realm AuthRealm

在认证、授权内部实现机制中都有提到,最终处理都将交给Real进行处理。因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.itcast.jk.shiro;

import java.util.List;

import org.apache.log4j.Logger;
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.UsernamePasswordToken;
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 cn.itcast.jk.domain.User;
import cn.itcast.jk.service.UserService;


public class AuthRealm extends AuthorizingRealm{
private static Logger log = Logger.getLogger(AuthRealm.class);
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}

//授权
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
log.info("执行授权...");

//获取登录用户的权限,配合jsp页面中的shiro标签进行控制
User curUser = (User) principals.fromRealm(getName()).iterator().next();
String userName = curUser.getUsername();
List<String> sList = userService.getPermission(userName );
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
for(String permission : sList){
//设置当前用户的权限
authorizationInfo.addStringPermission(permission);
}

return authorizationInfo;
}

//认证
/*
* shiro规则,按用户名查找,如果没找到返回null,如果查找到返回密码,shiro自动和调用subject.login()中的密码值进行比较
* 密码一致,登录成功;密码不一致,密码错误。
*/
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
log.info("执行认证...");

UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token;
if(usernamePasswordToken.getPassword()==null){ //从logout退出时
return null;
}
User findUser = new User(usernamePasswordToken.getUsername(), String.valueOf(usernamePasswordToken.getPassword()));
User user = userService.login(findUser);
if(user == null){ //该用户不存在
return null;
}else{ //用户存在
//SecurityUtils.getSubject().getSession().setAttribute("authUser", user);

//返回密码,自动比较密码
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(),getName());

return authenticationInfo;
}
}

}

6、修改传统登录为shiro登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.itcast.jk.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;

import cn.itcast.common.SysConstant;
import cn.itcast.jk.service.UserService;

/**
* @Description: 登录和推出类
* @Author: humingfeng
*/
public class LoginAction extends BaseAction {

private static final long serialVersionUID = 1L;

private String username;
private String password;

private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}

public String login() throws Exception {
/*
* shiro登录方式:根据用户名获取密码,密码为null非法用户;有密码检查是否用户填写的密码
* 登录成功后无需往httpsession中存放当前用户,这样就跟web容器绑定,关联太紧密;它自己创建
* subject对象,实现自己的session。这个跟web容器脱离,实现松耦合。
*/
Subject subject = SecurityUtils.getSubject(); //当前对象
//封装用户名密码到subject
//String password_cipherText= new Md5Hash(password,username+salt,2).toBase64();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//登录
try {
subject.login(token); //它会自动执行配置的realm中的doGetAuthenticationInfo

//将当前用户信息存放到session中;subject.getPrincipal()从shiro中获取查询到的用户对象
session.put(SysConstant.CURRENT_USER_INFO, subject.getPrincipal());

return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return "login";
}
}

public String logout(){
session.remove(SysConstant.CURRENT_USER_INFO); //删除session
return "logout";
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

7、授权

根据用户查询出角色对应的权限,并返回权限串
-hql,service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public List<String> getPermission(String userName) {
List<String> _list = new ArrayList<String>();

//用户,角色,权限,两级多对多,使用left join关联实现
String hql = "select p from User as u left join u.roles as r left join r.modules as p where u.username='"+userName+"'";
List<Module> moduleList = baseDao.find(hql, Module.class, null);

for(Module m : moduleList){
if(m!=null){ //观察hibernate实现的SQL,会多出一条Null记录
_list.add(m.getName());
}
}

return _list;
}

在realm中进行授权userService.getPermission

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//授权
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
log.info("执行授权...");

//获取登录用户的权限,配合jsp页面中的shiro标签进行控制
User curUser = (User) principals.fromRealm(getName()).iterator().next();
String userName = curUser.getUsername();
List<String> sList = userService.getPermission(userName );
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
for(String permission : sList){
//设置当前用户的权限
authorizationInfo.addStringPermission(permission);
}

return authorizationInfo;
}

8、页面使用shiro标签,/home/title.jsp 主菜单

1
2
3
4
5
6
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>


<shiro:hasPermission name="sysadmin">
<span id="topmenu" οnclick="toModule('sysadmin');">系统管理</span>
</shiro:hasPermission>

shiro登录机制:按用户名获取user,然后再交给shiro核实密码。核实时会匹配加密算法

执行顺序
1)action调用shiro的登录

1
2
3
4
5
6
7
8
9
10
try{
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
subject.login(token);
session.put(SysConstant.CURRENT_USER_INFO, subject.getPrincipal());
return SUCCESS;
}catch(Exception ex){
ActionContext.getContext().put("errorInfo", "用户名密码不正确,请重新填写!");
return "login";
}

2)调用自定义的密码匹配算法验证
根据和spring整合的shiro配置,找到子定义密码匹配类CustomCredentialsMatcher
appliactionContext-shiro.xml配置文件中

1
2
3
4
5
6
7
8
<!-- 自定义权限认证 -->
<bean id="authRealm" class="cn.itcast.jk.shiro.AuthRealm">
<property name="userService" ref="userService"/>
<property name="credentialsMatcher" ref="passwordMatcher"/>
</bean>

<!-- 设置密码加密策略 -->
<bean id="passwordMatcher" class="cn.itcast.jk.shiro.CustomCredentialsMatcher"/>

Apache Shiro具有以下特点:

  • 支持 -Apache Shiro是Apache软件基金会成员,这是一个公认为了社区利益最大化而行动的组织。
    PS:希望大家能通过文章可以学以致用,欢迎大家交流

本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:https://royalscholar.cn/2017/09/02/《伸手系列》第一集-Shiro安全认证框架的从入门到“出门”/

# Shiro

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×