当前位置:   article > 正文

Apache Shiro 简介_apache shiro版本查看

apache shiro版本查看

1.概述

在本文中,我们将介绍Apache Shiro,一个通用的 Java 安全框架。

该框架是高度可定制和模块化的,因为它提供了身份验证、授权、加密和会话管理。

2.依赖

Apache Shiro 有很多模块。但是,在本教程中,我们仅使用shiro-core工件。

让我们将它添加到我们的pom.xml中:

  1. <dependency>
  2. <groupId>org.apache.shiro</groupId>
  3. <artifactId>shiro-core</artifactId>
  4. <version>1.4.0</version>
  5. </dependency>

最新版本的 Apache Shiro 模块可以在 Maven Central 上找到。

3. 配置安全管理器

SecurityManager是 Apache Shiro 框架的核心部分。应用程序通常会运行它的单个实例。

在本教程中,我们将在桌面环境中探索该框架。要配置框架,我们需要在资源文件夹中创建一个shiro.ini文件,内容如下:

  1. [users]
  2. user = password, admin
  3. user2 = password2, editor
  4. user3 = password3, author
  5. [roles]
  6. admin = *
  7. editor = articles:*
  8. author = articles:compose,articles:save

shiro.ini配置文件的[users]部分定义了SecurityManager识别的用户凭据。格式为:rincipal (username) = password, role1, role2, ..., role

角色及其相关权限在[roles]部分中声明。管理员角色被授予对应用程序每个部分的权限和访问权限。这由通配符(*)符号表示。

编辑角色拥有与文章相关的所有权限,而作者角色只能撰写保存文章。

SecurityManager用于配置SecurityUtils类。从SecurityUtils我们可以获取当前与系统交互的用户,并进行认证和授权操作。

让我们使用IniRealmshiro.ini文件加载我们的用户和角色定义,然后使用它来配置DefaultSecurityManager对象:

  1. IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
  2. SecurityManager securityManager = new DefaultSecurityManager(iniRealm);
  3. SecurityUtils.setSecurityManager(securityManager);
  4. Subject currentUser = SecurityUtils.getSubject();

现在我们有了一个知道shiro.ini文件中定义的用户凭据和角色的SecurityManager,让我们继续进行用户身份验证和授权。

4. 认证

在 Apache Shiro 的术语中,Subject是与系统交互的任何实体。它可以是人、脚本或 REST 客户端。

调用SecurityUtils.getSubject()返回当前Subject的一个实例,即currentUser

现在我们有了currentUser对象,我们可以对提供的凭据执行身份验证:

  1. if (!currentUser.isAuthenticated()) {
  2. UsernamePasswordToken token
  3. = new UsernamePasswordToken("user", "password");
  4. token.setRememberMe(true);
  5. try {
  6. currentUser.login(token);
  7. } catch (UnknownAccountException uae) {
  8. log.error("Username Not Found!", uae);
  9. } catch (IncorrectCredentialsException ice) {
  10. log.error("Invalid Credentials!", ice);
  11. } catch (LockedAccountException lae) {
  12. log.error("Your Account is Locked!", lae);
  13. } catch (AuthenticationException ae) {
  14. log.error("Unexpected Error!", ae);
  15. }
  16. }

首先,我们检查当前用户是否尚未通过身份验证。然后我们使用用户的主体(用户名)和凭证(密码)创建一个身份验证令牌。

接下来,我们尝试使用令牌登录。如果提供的凭据是正确的,那么一切都会好起来的。

不同的情况有不同的例外。也可以抛出更适合应用程序要求的自定义异常。这可以通过继承 AccountException类来完成。

5.授权

身份验证试图验证用户的身份,而授权试图控制对系统中某些资源的访问。

回想一下,我们为在shiro.ini文件中创建的每个用户分配了一个或多个角色。此外,在角色部分,我们为每个角色定义了不同的权限或访问级别。

现在让我们看看如何在我们的应用程序中使用它来实施用户访问控制。

shiro.ini文件中,我们赋予管理员对系统每个部分的完全访问权限。

编辑可以完全访问有关文章的每个资源/操作,并且作者仅限于撰写和保存文章

让我们根据角色欢迎当前用户:

  1. if (currentUser.hasRole("admin")) {
  2. log.info("Welcome Admin");
  3. } else if(currentUser.hasRole("editor")) {
  4. log.info("Welcome, Editor!");
  5. } else if(currentUser.hasRole("author")) {
  6. log.info("Welcome, Author");
  7. } else {
  8. log.info("Welcome, Guest");
  9. }

现在,让我们看看当前用户在系统中被允许做什么:

  1. if(currentUser.isPermitted("articles:compose")) {
  2. log.info("You can compose an article");
  3. } else {
  4. log.info("You are not permitted to compose an article!");
  5. }
  6. if(currentUser.isPermitted("articles:save")) {
  7. log.info("You can save articles");
  8. } else {
  9. log.info("You can not save articles");
  10. }
  11. if(currentUser.isPermitted("articles:publish")) {
  12. log.info("You can publish articles");
  13. } else {
  14. log.info("You can not publish articles");
  15. }

6. 领域配置

在实际应用程序中,我们需要一种从数据库而不是从shiro.ini文件中获取用户凭据的方法。这就是 Realm 概念发挥作用的地方。

在 Apache Shiro 的术语中,Realm是一个 DAO,它指向身份验证和授权所需的用户凭据存储。

要创建领域,我们只需要实现领域接口。这可能很乏味;但是,该框架带有默认实现,我们可以从中继承。这些实现之一是JdbcRealm

我们创建了一个自定义领域实现,它扩展了 JdbcRealm类并覆盖了以下方法:doGetAuthenticationInfo()doGetAuthorizationInfo()getRoleNamesForUser()getPermissions()

让我们通过继承 JdbcRealm类来创建一个领域:

  1. public class MyCustomRealm extends JdbcRealm {
  2. //...
  3. }

为了简单起见,我们使用java.util.Map来模拟一个数据库:

  1. private Map<String, String> credentials = new HashMap<>();
  2. private Map<String, Set<String>> roles = new HashMap<>();
  3. private Map<String, Set<String>> perm = new HashMap<>();
  4. {
  5. credentials.put("user", "password");
  6. credentials.put("user2", "password2");
  7. credentials.put("user3", "password3");
  8. roles.put("user", new HashSet<>(Arrays.asList("admin")));
  9. roles.put("user2", new HashSet<>(Arrays.asList("editor")));
  10. roles.put("user3", new HashSet<>(Arrays.asList("author")));
  11. perm.put("admin", new HashSet<>(Arrays.asList("*")));
  12. perm.put("editor", new HashSet<>(Arrays.asList("articles:*")));
  13. perm.put("author",
  14. new HashSet<>(Arrays.asList("articles:compose",
  15. "articles:save")));
  16. }

让我们继续并覆盖doGetAuthenticationInfo()

  1. protected AuthenticationInfo
  2. doGetAuthenticationInfo(AuthenticationToken token)
  3. throws AuthenticationException {
  4. UsernamePasswordToken uToken = (UsernamePasswordToken) token;
  5. if(uToken.getUsername() == null
  6. || uToken.getUsername().isEmpty()
  7. || !credentials.containsKey(uToken.getUsername())) {
  8. throw new UnknownAccountException("username not found!");
  9. }
  10. return new SimpleAuthenticationInfo(
  11. uToken.getUsername(),
  12. credentials.get(uToken.getUsername()),
  13. getName());
  14. }

我们首先将提供的AuthenticationToken转换为UsernamePasswordToken。从uToken中,我们提取用户名(uToken.getUsername())并使用它从数据库中获取用户凭据(密码)。

如果没有找到记录——我们抛出一个UnknownAccountException,否则我们使用凭证和用户名来构造一个从该方法返回的SimpleAuthenticatioInfo对象。

如果用户凭证使用盐进行哈希处理,我们需要返回一个SimpleAuthenticationInfo以及相关的盐:

  1. return new SimpleAuthenticationInfo(
  2. uToken.getUsername(),
  3. credentials.get(uToken.getUsername()),
  4. ByteSource.Util.bytes("salt"),
  5. getName()
  6. );

我们还需要覆盖doGetAuthorizationInfo()以及getRoleNamesForUser()getPermissions()

最后,让我们将自定义领域插入到securityManager中。我们需要做的就是用我们的自定义领域替换上面的IniRealm,并将其传递给DefaultSecurityManager的构造函数:

  1. Realm realm = new MyCustomRealm();
  2. SecurityManager securityManager = new DefaultSecurityManager(realm);

代码的所有其他部分都与以前相同。这就是我们使用自定义领域正确配置securityManager所需的全部内容。

现在的问题是——框架如何匹配凭证?

默认情况下,JdbcRealm使用SimpleCredentialsMatcher ,它仅通过比较AuthenticationTokenAuthenticationInfo中的凭据来检查是否相等。

如果我们散列密码,我们需要通知框架使用HashedCredentialsMatcher。可以在此处找到具有散列密码的领域的 INI 配置。

7. 登出

现在我们已经验证了用户,是时候实现注销了。这只需调用一个方法即可完成——该方法使用户会话无效并将用户注销:

currentUser.logout();

8. 会话管理

该框架自然带有其会话管理系统。如果在 Web 环境中使用,则默认为HttpSession实现。

对于独立应用程序,它使用其企业会话管理系统。好处是即使在桌面环境中,您也可以像在典型 Web 环境中那样使用会话对象。

让我们看一个简单的示例并与当前用户的会话进行交互:

  1. Session session = currentUser.getSession();
  2. session.setAttribute("key", "value");
  3. String value = (String) session.getAttribute("key");
  4. if (value.equals("value")) {
  5. log.info("Retrieved the correct value! [" + value + "]");
  6. }

9. Shiro 用于使用 Spring 的 Web 应用程序

到目前为止,我们已经概述了 Apache Shiro 的基本结构,并且我们已经在桌面环境中实现了它。让我们继续将框架集成到 Spring Boot 应用程序中。

请注意,这里的主要焦点是 Shiro,而不是 Spring 应用程序——我们只会使用它来支持一个简单的示例应用程序。

9.1。依赖项

首先,我们需要将 Spring Boot 父依赖添加到我们的pom.xml中:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.7.2</version>
  5. </parent>

接下来,我们必须将以下依赖项添加到同一个pom.xml文件中:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-freemarker</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.apache.shiro</groupId>
  11. <artifactId>shiro-spring-boot-web-starter</artifactId>
  12. <version>${apache-shiro-core-version}</version>
  13. </dependency>

9.2. 配置

shiro-spring-boot-web-starter依赖添加到我们的pom.xml将默认配置 Apache Shiro 应用程序的一些功能,例如SecurityManager

但是,我们仍然需要配置Realm和 Shiro 安全过滤器。我们将使用上面定义的相同自定义领域。

因此,在运行 Spring Boot 应用程序的主类中,让我们添加以下Bean定义:

  1. @Bean
  2. public Realm realm() {
  3. return new MyCustomRealm();
  4. }
  5. @Bean
  6. public ShiroFilterChainDefinition shiroFilterChainDefinition() {
  7. DefaultShiroFilterChainDefinition filter
  8. = new DefaultShiroFilterChainDefinition();
  9. filter.addPathDefinition("/secure", "authc");
  10. filter.addPathDefinition("/**", "anon");
  11. return filter;
  12. }

ShiroFilterChainDefinition中,我们将authc过滤器应用于/secure路径,并使用 Ant 模式将anon过滤器应用于其他路径。

Web 应用程序默认提供 authc和anon过滤器。其他默认过滤器可以在这里找到。

如果我们没有定义Realm bean,ShiroAutoConfiguration将默认提供一个IniRealm实现,该实现期望在src/main/resourcessrc/main/resources/META-INF中找到一个shiro.ini文件。

如果我们不定义ShiroFilterChainDefinition bean,框架会保护所有路径并将登录 URL 设置为login.jsp

我们可以通过将以下条目添加到我们的application.properties来更改此默认登录 URL 和其他默认值:

  1. shiro.loginUrl = /login
  2. shiro.successUrl = /secure
  3. shiro.unauthorizedUrl = /login

现在authc过滤器已应用于/secure,对该路由的所有请求都需要表单身份验证。

9.3. 认证和授权

让我们创建一个具有以下路径映射的ShiroSpringController : /index/login、/logout/secure。

login()方法是我们实现如上所述的实际用户身份验证的地方。如果身份验证成功,用户将被重定向到安全页面:

  1. Subject subject = SecurityUtils.getSubject();
  2. if(!subject.isAuthenticated()) {
  3. UsernamePasswordToken token = new UsernamePasswordToken(
  4. cred.getUsername(), cred.getPassword(), cred.isRememberMe());
  5. try {
  6. subject.login(token);
  7. } catch (AuthenticationException ae) {
  8. ae.printStackTrace();
  9. attr.addFlashAttribute("error", "Invalid Credentials");
  10. return "redirect:/login";
  11. }
  12. }
  13. return "redirect:/secure";

现在在secure()实现中,currentUser是通过调用SecurityUtils.getSubject() 获得的。用户的角色和权限以及用户的主体被传递到安全页面:

  1. Subject currentUser = SecurityUtils.getSubject();
  2. String role = "", permission = "";
  3. if(currentUser.hasRole("admin")) {
  4. role = role + "You are an Admin";
  5. } else if(currentUser.hasRole("editor")) {
  6. role = role + "You are an Editor";
  7. } else if(currentUser.hasRole("author")) {
  8. role = role + "You are an Author";
  9. }
  10. if(currentUser.isPermitted("articles:compose")) {
  11. permission = permission + "You can compose an article, ";
  12. } else {
  13. permission = permission + "You are not permitted to compose an article!, ";
  14. }
  15. if(currentUser.isPermitted("articles:save")) {
  16. permission = permission + "You can save articles, ";
  17. } else {
  18. permission = permission + "\nYou can not save articles, ";
  19. }
  20. if(currentUser.isPermitted("articles:publish")) {
  21. permission = permission + "\nYou can publish articles";
  22. } else {
  23. permission = permission + "\nYou can not publish articles";
  24. }
  25. modelMap.addAttribute("username", currentUser.getPrincipal());
  26. modelMap.addAttribute("permission", permission);
  27. modelMap.addAttribute("role", role);
  28. return "secure";

我们完成了。这就是我们如何将 Apache Shiro 集成到 Spring Boot 应用程序中。

另外,请注意,该框架提供了额外的注释,可以与过滤器链定义一起使用以保护我们的应用程序。

10. JEE 集成

将 Apache Shiro 集成到 JEE 应用程序中只需配置web.xml文件。像往常一样,配置期望shiro.ini在类路径中。此处提供了详细的示例配置。此外,可以在此处找到 JSP 标记。

11. 结论

在本教程中,我们研究了 Apache Shiro 的身份验证和授权机制。我们还专注于如何定义自定义领域并将其插入SecurityManager

与往常一样,完整的源代码可在 GitHub 上获得。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/570326
推荐阅读
相关标签
  

闽ICP备14008679号