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-hk/n/361251.html