Java 安全框架
作者:网络转载 发布时间:[ 2017/3/8 10:49:55 ] 推荐标签:测试开发技术 Java
编程式授权 基于角色的访问控制(RBAC) baseRole.ini
[users]
# 等号右边第一个为密码,后面为角色
lee1=123, role1
lee2=456, role1, role2
角色控制
RoleTest.java
import com.lee.shiro.util.ShiroUtils;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class RoleTest {
@Test
public void HasRoleTest() {
Subject currentUser = ShiroUtils.login("classpath:shiroRole.ini",
"lee1", "123");
System.out.println(currentUser.hasRole("role1"));
}
}
Subject 判断当前用户是否具有某角色的方法
返回值为布尔类型的
boolean hasRole(String roleIdentifier) boolean[] hasRoles(List<String> roleIdentifiers) boolean hasAllRoles(Collection<String> roleIdentifiers)
没有返回值但抛异常的
void checkRole(String roleIdentifier) throws AuthorizationException void checkRoles(Collection<String> roleIdentifiers) throws AuthorizationException void checkRoles(String... roleIdentifiers) throws AuthorizationException 基于权限的访问控制 basePermisssion.ini
[users]
# 等号右边第一个为密码,后面为角色
lee1=123, role1
lee2=456, role1, role2
[roles]
role1=user:select
role2=user:add, user:update, user:delete
权限控制
PermissionTest.java
import com.lee.shiro.util.ShiroUtils;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class PermissionTest {
@Test
public void isPermitted() {
Subject currentUser = ShiroUtils.login("classpath:basePermission.ini",
"lee1", "123");
System.out.println(currentUser.isPermitted("user:select"));
}
}
Subject 判断当前用户是否具有某权限的方法
返回值为布尔类型的
boolean isPermitted(Permission permission) boolean[] isPermitted(String... permissions) boolean[] isPermitted(List<Permission> permissions) boolean isPermittedAll(String... permissions) boolean isPermittedAll(Collection<Permission> permissions)
没有返回值但抛异常的
void checkPermission(String permission) throws AuthorizationException void checkPermission(Permission permission) throws AuthorizationException void checkPermissions(String... permissions) throws AuthorizationException void checkPermissions(Collection<Permission> permissions) throws AuthorizationException 注解式授权
基于注解授权
@RequiresAuthentication
要求当前 Subject 已经在当前的 session 中被验证通过才能被访问或调用。
@RequiresAuthentication
public void updateAccount(Account userAccount) {
//this method will only be invoked by a
//Subject that is guaranteed authenticated
...
}
等同于
public void updateAccount(Account userAccount) {
if (!SecurityUtils.getSubject().isAuthenticated()) {
throw new AuthorizationException(...);
}
//Subject is guaranteed authenticated here
...
}
@RequiresGuest
要求当前的 Subject 是一个“guest”,也是说,他们必须是在之前的 session 中没有被验证或被记住才能被访问或调用。
@RequiresGuest
public void signUp(User newUser) {
//this method will only be invoked by a
//Subject that is unknown/anonymous
...
}
等同于
public void signUp(User newUser) {
Subject currentUser = SecurityUtils.getSubject();
PrincipalCollection principals = currentUser.getPrincipals();
if (principals != null && !principals.isEmpty()) {
//known identity - not a guest:
throw new AuthorizationException(...);
}
//Subject is guaranteed to be a 'guest' here
...
}
@RequiresPermissions(“account:create”)
要求当前的 Subject 被允许一个或多个权限,以便执行注解方法。
@RequiresPermissions("account:create")
public void createAccount(Account account) {
//this method will only be invoked by a Subject
//that is permitted to create an account
...
}
等同于
public void createAccount(Account account) {
Subject currentUser = SecurityUtils.getSubject();
if (!subject.isPermitted("account:create")) {
throw new AuthorizationException(...);
}
//Subject is guaranteed to be permitted here
...
}
@RequiresRoles(“administrator”)
要求当前的 Subject 拥有所有指定的角色,如果他们没有,则该方法将不会被执行,而且AuthorizationException异常将会被抛出。
@RequiresRoles("administrator")
public void deleteUser(User user) {
//this method will only be invoked by an administrator
...
}
等同于
public void deleteUser(User user) {
Subject currentUser = SecurityUtils.getSubject();
if (!subject.hasRole("administrator")) {
throw new AuthorizationException(...);
}
//Subject is guaranteed to be an 'administrator' here
...
}
@RequiresUser
注解需要当前的 Subject 是一个应用程序用户才能被注解的类/实例方法访问或调用。一个“应用程序用户”被定义为一个拥有已知身份,或在当前 session 中通过验证确认,或者在之前 session 中的“RememberMe”服务被记住。
@RequiresUser
public void updateAccount(Account account) {
//this method will only be invoked by a 'user'
//i.e. a Subject with a known identity
...
}
等同于
public void updateAccount(Account account) {
Subject currentUser = SecurityUtils.getSubject();
PrincipalCollection principals = currentUser.getPrincipals();
if (principals == null || principals.isEmpty()) {
//no identity - they're anonymous, not allowed:
throw new AuthorizationException(...);
}
//Subject is guaranteed to have a known identity here
...
}
JSP 标签授权
JSP 标签授权需要导入shiro-web.jar,并添加标签:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
The guest tag
用户没有身份验证时显示相应信息,即游客访问信息。
<shiro:guest>
Hi there! Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!
</shiro:guest>
The user tag
用户已经身份验证/记住我登录后显示相应的信息。
<shiro:user>
Welcome back John! Not John? Click <a href="login.jsp">here<a> to login.
</shiro:user>
The authenticated tag
用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的。
<shiro:authenticated>
<a href="updateAccount.jsp">Update your contact information</a>.
</shiro:authenticated>
The notAuthenticated tag
用户没有身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证。
<shiro:notAuthenticated>
Please <a href="login.jsp">login</a> in order to update your credit card information.
</shiro:notAuthenticated>
The principal tag
显示用户身份信息,默认调用 Subject.getPrincipal() 获取,即 Primary Principal。
Hello, <shiro:principal/>, how are you today?
等同于
Hello, <%= SecurityUtils.getSubject().getPrincipal().toString() %>, how are you today?
Principal property
Hello, <shiro:principal property="firstName"/>, how are you today?
Hello, <%= SecurityUtils.getSubject().getPrincipal().getFirstName().toString() %>, how are you today?
Hello, <shiro:principal type="com.foo.User" property="firstName"/>, how are you today?
Hello, <%= SecurityUtils.getSubject().getPrincipals().oneByType(com.foo.User.class).getFirstName().toString() %>, how are you today?
The hasRole tag
如果当前 Subject 有此角色将显示 body 内容。
<shiro:hasRole name="administrator">
<a href="admin.jsp">Administer the system</a>
</shiro:hasRole>
The lacksRole tag
如果当前 Subject 没有角色将显示 body 内容。
<shiro:lacksRole name="administrator">
Sorry, you are not allowed to administer the system.
</shiro:lacksRole>
The hasAnyRole tag
如果当前 Subject 有任意一个角色(或关系),将显示 body 内容。
<shiro:hasAnyRoles name="developer, project manager, administrator">
You are either a developer, project manager, or administrator.
</shiro:lacksRole>
The hasPermission tag
如果当前 Subject 有权限将显示 body 内容。
<shiro:hasPermission name="user:create">
<a href="createUser.jsp">Create a new User</a>
</shiro:hasPermission>
The lacksPermission tag
如果当前 Subject 没有权限将显示 body 内容。
<shiro:lacksPermission name="user:delete">
Sorry, you are not allowed to delete user accounts.
</shiro:hasPermission>
集成 Web
此处只列举关键代码,若要查看详细代码移步到GitHub
依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
web.xml
<!-- 若使用 classpath 则需在此定义,否则将此去掉 -->
<context-param>
<param-name>shiroConfigLocations</param-name>
<param-value>classpath:shiroWeb.ini</param-value>
</context-param>
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
<!-- 若配置文件在 /WEB-INF/ 下则在此配置
<init-param>
<param-name>configPath</param-name>
<param-value>/WEB-INF/shiroWeb.ini</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
shiroWeb.ini
# authc、roles 等都代表着一个 Filter,具体意义看 Default Filters
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Java性能测试有哪些不为众人所知的原则?Java设计模式??装饰者模式谈谈Java中遍历Map的几种方法Java Web入门必知你需要理解的Java反射机制知识总结编写更好的Java单元测试的7个技巧编程常用的几种时间戳转换(java .net 数据库)适合Java开发者学习的Python入门教程Java webdriver如何获取浏览器新窗口中的元素?Java重写与重载(区别与用途)Java变量的分类与初始化JavaScript有这几种测试分类Java有哪四个核心技术?给 Java开发者的10个大数据工具和框架Java中几个常用设计模式汇总java生态圈常用技术框架、开源中间件,系统架构及经典案例等
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南