Shiro是什麼?

Shiro是一款開源的Java安全框架,可以為任何應用程序提供保護,並為其提供身份驗證(authentication)、授權(authorization)、密碼學(cryptography)、會話管理(session management)等功能。Shiro的主要目標是使安全容易使用。

一、Shiro是什麼意思

Shiro這個詞是日語,意思是「城」或「城市」,這個名字意味著Shiro的使命是保護應用程序和組織就像城牆保護城市一樣。通過Shiro框架,開發人員可以輕鬆地為應用程序添加安全性,從而保護應用程序的數據和資源。

二、Shiraz是什麼意思

與Shiro類似,Shiraz也是一個城市的名字,位於伊朗西南部。但是與Shiro無關。

三、Shiro是什麼牌子

Shiro不是一家公司或品牌的名字,而是一個開源軟體框架的名字,由Apache Software Foundation主持管理和開發。

四、Shire是什麼

Shire通常指的是英國地名,但是在計算機領域,Shire可以指代Shiro的一個子項目,即Shiro-integration-test。這個子項目用於測試Shiro與各種Web框架的集成。

五、Shiro是什麼技術

Shiro是一種Java安全框架,它不依賴於任何其他的框架或技術。它是一個獨立的框架,可以與任何其他Java技術一起使用,例如Spring、Hibernate等。

六、Shirr是什麼意思

Shirr這個詞語指的是將面料或紙張縱向收攏起來,用於增強強度或形狀的過程。但是與Shiro無關。

七、Shiro是什麼設備

Shiro不是一種設備,而是一種軟體框架。

八、Shiro框架是什麼

Shiro框架是一個Java安全框架,用於保護企業級應用程序的安全。Shiro可以為應用程序提供身份驗證、授權、密碼學、會話管理等功能。它是一個易於使用的框架,可以輕鬆地與任何其他Java技術一起使用。

九、Shiro是什麼意思中文

Shiro的中文意思是「城」或「城市」。正如其名所示,Shiro旨在為您的應用程序提供安全保護,就像城牆保護城市一樣,它可以為您的應用程序提供保護,以避免被黑客攻擊。

十、Shiro是什麼牌子中文

Shiro不是一個商標或品牌名,而是一個開源軟體框架的名字,由Apache主持管理和開發。

Shiro示例代碼:

#pom.xml文件中添加shiro依賴
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.7.0</version>
</dependency>

#創建.ini文件用於存儲用戶信息和角色信息
[users]
username=password,role1,role2
username2=password2,role2

[roles]
role1=permission1,permission2
role2=permission2,permission3

#創建Shiro配置類
public class ShiroConfiguration {

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customRealm());
        return securityManager;
    }

    @Bean
    public CustomRealm customRealm() {
        CustomRealm customRealm = new CustomRealm();
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return customRealm;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }
}

#創建自定義Realm
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    //授權
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) principals.getPrimaryPrincipal();
        User user = userService.findByUsername(username);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(user.getRoles());
        authorizationInfo.setStringPermissions(user.getPermissions());
        return authorizationInfo;
    }

    //身份驗證 
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new UnknownAccountException();
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());
        return authenticationInfo;
    }
}

#創建自定義Filter
public class CustomRolesAuthorizationFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        Subject subject = getSubject(request, response);
        String[] rolesArray = (String[]) mappedValue;
        if (rolesArray == null || rolesArray.length == 0) {
            return true;
        }
        for (String roleName : rolesArray) {
            if (subject.hasRole(roleName)) {
                return true;
            }
        }
        return false;
    }
}

#配置自定義Filter
public class ShiroFilterConfiguration {

    @Bean
    public FilterRegistrationBean registration(CustomRolesAuthorizationFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }

    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/admin/**", "roles[admin]");
        filterChainDefinitionMap.put("/guest/**", "roles[guest]");
        filterChainDefinitionMap.put("/user/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        Map<String,Filter> filters = new HashMap<>();
        filters.put("roles", new CustomRolesAuthorizationFilter());
        shiroFilterFactoryBean.setFilters(filters);
        return shiroFilterFactoryBean;
    }
}

#創建用戶Service
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }
}

#創建UserDao
@Repository
public class UserDao {

    private Map<String,String> userMap = new HashMap<>();

    {
        userMap.put("admin", "e10adc3949ba59abbe56e057f20f883e");
        userMap.put("guest", "e10adc3949ba59abbe56e057f20f883e");
    }

    public User findByUsername(String username) {
        String password = userMap.get(username);
        if (password == null) {
            return null;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRoles(new HashSet<>(Arrays.asList("admin", "guest"))));
        user.setPermissions(new HashSet<>(Arrays.asList("permission1", "permission2", "permission3"))));
        return user;
    }
}

#創建User類
public class User {

    private String username;
    private String password;
    private Set<String> roles;
    private Set<String> permissions;

    //getter和setter方法省略
}

#在Controller中使用Shiro註解
@Controller
@RequestMapping("/user")
public class UserController {

    @RequiresRoles("admin")
    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }

    @RequiresRoles("guest")
    @RequestMapping("/guest")
    public String guest() {
        return "guest";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }
}

原創文章,作者:OONNZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/361251.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
OONNZ的頭像OONNZ
上一篇 2025-02-24 00:34
下一篇 2025-02-24 00:34

相關推薦

  • 深度解析Apache Shiro Subject

    一、Shiro Subject的簡介 Apache Shiro是一個功能強大且易於使用的Java安全框架,提供身份驗證(認證)、授權、加密和會話管理等功能,可以輕鬆地為Web、移動…

    編程 2025-04-12
  • 深入淺出:shiro註解

    Shiro是一個功能強大的開源Java安全框架,它為我們提供了很多安全特性,如身份認證、授權等。在Shiro中,註解是一種很方便的方式來添加安全特性,其中包括身份認證、授權、加密以…

    編程 2025-04-12
  • Spring Boot集成Shiro指南

    一、什麼是Shiro? Shiro是一個強大、易用的Java安全框架,用於身份驗證、授權、密碼和會話管理。Shiro的優點是簡單直接,能夠輕鬆地集成到任何Java應用程序中。 二、…

    編程 2025-02-11
  • 如何實現Shiro的登出功能

    一、Shiro登出功能的概述 Shiro是一個非常流行的Java安全框架,它提供了很多安全特性,比如身份驗證、授權、加密等等。在一個Web應用中,很多時候用戶需要登出系統,這時候就…

    編程 2025-02-05
  • Spring Boot集成Shiro實現許可權管理

    一、概述 Shiro是一個Java安全框架,提供了身份驗證、授權、密碼加密和會話管理等功能。可以集成到任何Java應用程序中,包括Web、REST API和大數據架構等。Sprin…

    編程 2024-12-30
  • shiro.ini:引領安全之路

    shiro是一個Java安全框架,廣泛用於企業級應用程序開發。它負責身份驗證、授權、密碼解密、會話管理和加密等方面。shiro被認為是目前最穩健、最全面的Java安全框架之一,sh…

    編程 2024-11-09
  • SimpleAuthenticationInfo 在 Shiro 中的作用和實現

    一、SimpleAuthenticationInfo 簡介 SimpleAuthenticationInfo 是 Shiro 框架的核心組件之一,用於封裝身份驗證信息,實現自定義的…

    編程 2024-11-02
  • Shiro配置不攔截請求

    Shiro是一個用於身份驗證、授權、加密、會話管理等的Java的安全框架,為Java應用程序提供安全認證和授權支持。在實際開發過程中,除了需要進行身份驗證和授權認證外,還需要對一些…

    編程 2024-10-04
  • 深入淺出 Shiro 許可權管理

    Apache Shiro 是一個強大且易於使用的 Java 安全框架,它提供了身份驗證、授權、加密等許多常見的安全功能。在今天這個信息化時代,隨著互聯網業務的快速發展和應用的廣泛普…

    編程 2024-10-04

發表回復

登錄後才能評論