Shiro 配置
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
web.xml:
<!-- Shiro配置 --><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>shiro.xml:
<description>Shiro Configuration</description><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="myRealm"/><!-- 使用下面配置的緩存管理器 --><property name="cacheManager" ref="cacheManager"/></bean><!--自定義Realm--><bean id="myRealm" class="com.hssArray.security.shiro.MyRealm"/><!-- 配置shiro的過濾器工廠類,id- shiroFilter要和我們?cè)趙eb.xml中配置的過濾器一致 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><!-- 調(diào)用我們配置的權(quán)限管理器 --><property name="securityManager" ref="securityManager"/><!-- 配置我們的登錄請(qǐng)求地址 --><property name="loginUrl" value="/login"/><!-- 配置我們?cè)诘卿涰?yè)登錄成功后的跳轉(zhuǎn)地址,如果你訪問的是非/login地址,則跳到您訪問的地址 --><property name="successUrl" value="/main"/><!-- 如果您請(qǐng)求的資源不再您的權(quán)限范圍,則跳轉(zhuǎn)到/403請(qǐng)求地址 --><property name="unauthorizedUrl" value="/403.jsp"/><!-- 權(quán)限配置 --><property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource"/><property name="filterChainDefinitions"><value>/js/** = anon</value> </property></bean><!--自定義filterChainDefinitionMap--><bean id="chainDefinitionSectionMetaSource" class="com.hssArray.security.shiro.ChainDefinitionSectionMetaSource"/><!--shiro緩存管理器--><bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>主要類:
public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section> {@Autowiredprivate FunctionService functionService;// 靜態(tài)資源訪問權(quán)限private String filterChainDefinitions = "/static/**=anon";@Overridepublic Ini.Section getObject() throws Exception {List<Function> list = functionService.findAll();Ini ini = new Ini();// 加載默認(rèn)的urlini.load(filterChainDefinitions);Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);// 循環(huán)Resource的url,逐個(gè)添加到section中。section就是filterChainDefinitionMap,// 里面的鍵就是鏈接URL,值就是存在什么條件才能訪問該鏈接for (Iterator<Function> it = list.iterator(); it.hasNext();) {Function function = it.next();// 構(gòu)成permission字符串if (StringUtils.hasText(function.getValue()) && StringUtils.hasText(function.getType())) {String permission = "";String t = function.getType();if (t.equals("anon")) {permission = "anon";} else if (t.equals("perms")) {permission = "perms[" + function.getPermission().getPermissionname() + "]";} else if (t.equals("roles")) {permission = "roles[" + function.getRole().getRolename() + "]";}section.put(function.getValue(), permission);}}// 所有資源的訪問權(quán)限,必須放在最后section.put("/**", "authc");return section;}@Overridepublic Class<?> getObjectType() {return this.getClass();}@Overridepublic boolean isSingleton() {return false;} } package com.hssArray.security.shiro;/*** 自定義Realm,進(jìn)行數(shù)據(jù)源配置** Created by Jeremie on 2014/10/1.*/@Service @Transactional public class MyRealm extends AuthorizingRealm {@Injectprivate UserService userService;/*** 獲取授權(quán)信息*/@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {// 獲取登錄時(shí)輸入的用戶名String loginName = (String) principalCollection.fromRealm(getName()).iterator().next();// 到數(shù)據(jù)庫(kù)獲取此用戶User user = userService.findByName(loginName);if (user != null) {// 權(quán)限信息對(duì)象info,用來存放查出的用戶的所有的角色(role)及權(quán)限(permission)SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();// 用戶的角色集合info.setRoles(user.getRolesName());// 用戶的角色對(duì)應(yīng)的所有權(quán)限,如果只使用角色定義訪問權(quán)限Collection<Role> roleList = user.getRoleList();for (Role role : roleList) {info.addStringPermissions(role.getPermissionsName());}return info;}return null;}/*** 獲取身份驗(yàn)證相關(guān)信息*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {// UsernamePasswordToken對(duì)象用來存放提交的登錄信息UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;// 查出是否有此用戶User user = userService.findByName(token.getUsername());if (user != null) {// 若存在,將此用戶存放到登錄認(rèn)證info中return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());}return null;}}轉(zhuǎn)載于:https://my.oschina.net/u/3503613/blog/1569017
總結(jié)