(一)登陸的處理java
1. 導入權限控制shiro相關的jar包web
<!-- Apache Shiro 權限架構 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.2.3</version> </dependency>
2. 在spring的配置文件applicationContent-shiro中添加5個bean的配置信息spring
<!--1. 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--注入原材料屬性 --> <property name="realm" ref="authRealm"></property> </bean> <!--2. 注入Realm原材料--> <bean id="authRealm" class="cn.tedu.shiro.AuthRealm"> <!--注入自定義的加密匹配器 --> <property name="credentialsMatcher" ref="authcredentialMacher"></property> </bean> <!--3.加密匹配器 --> <bean id="authcredentialMacher" class="cn.tedu.shiro.AuthcredentialMacher"> </bean> <!--4.權限認證通知 --> <bean id="advisor" class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <!-- 注入安全管理器 --> <property name="securityManager" ref="securityManagee"></property> </bean> <!--5.shiro過濾工廠 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" > <!-- 注入安全管理器 --> <property name="secrityManager" ref="securityManager"></property> <!-- 注入登陸地址 --> <property name="loginUrl" value="/tologin"></property> <!-- 注入攔截地址和放行地址 --> <property name="filterChainDefinitions"> <value> <!-- 放行登陸按鈕的請求地址 --> /login.action=anno <!-- 放行全部靜態資源文件 --> /staticfile/**=anno <!-- 攔截剩下的全部 --> /**authc </value> </property> </bean>
3. 在web.xml文件中 配置shiro過濾器數據庫
<!-- 配置shiro的過濾器 --> <filter> <!-- 此處name必定要和spring配置文件中的過濾工廠id一致 --> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <!-- 配置讓shiro的過濾工廠的生命週期 交由web容器進行管理 --> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4. 在登陸controller中建立subject對象apache
//1.建立Subject Subject subject = SecurityUtils.getSubject(); //2.建立用戶名密碼的令牌 UsernamePasswordToken token = new UsernamePasswordToken(userName, password); //3.登陸 try { //若是成功 則無異常 登陸失敗會拋出異常 subject.login(token); //獲得詳細用戶對象存到session中 User user = (User) subject.getPrincipal(); user = userService.findOne(user.getUserId()); session.setAttribute("_CURRENT_USER", user); return "redirect:/home.action"; } catch (AuthenticationException exception) {//登陸失敗 model.addAttribute("errorInfo", "用戶名或密碼錯誤!"); model.addAttribute("username", userName); model.addAttribute("password",password); return "sysadmin/login/login"; }
5. 提早準備好,下面3個類:安全
public class AuthRealm extends SimpleAccountRealm{ @Autowired private UserService userService; //給登陸提供素材 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) { UsernamePasswordToken upToken=(UsernamePasswordToken) token; //principal:正確的用戶對象 User user =userService.findUserByUsername(upToken.getUsername()); //credentials:正確的用戶密碼 //realmName:原材料類的名稱 AuthenticationInfo info=new SimpleAuthenticationInfo( user,user.getPassword(),this.getName()); return info; } Service層: @Override public User findUserByUsername(String username) { return userMapper.findUserByUsername(username); } Dao層: @Select("select * from user_p where username=#username") public User findUserByUsername(String username);
public class Md5Utils{ public static String getMd5(String password){ Md5Hash md5Hash = new Md5Hash(password, "tedu", 3); return md5Hash.toString(); } }
//加密匹配器,須要調用Md5Utils對輸入的密碼進行加密後和數據庫中比較驗證 public class AuthcredentialMacher extends SimpleCredentialsMatcher{ public boolean doGredentialsMatch(AuthenticationToken token,AuthenticationInfo info){ UsernamePasswordToken uptoken =(UsernamePasswordToken) token; //對用戶輸入的密碼進行加密 String newPassword = Md5Utils.getMd5(String.valueOf(uptoken.getPassword())); //把加密後的密碼設置回token中 uptoken.setPassword(newPassword.toCharArray()); return super.doCredentialsMatch(token, info); }
(二)實現shiro進行權限控制session
//給權限控制提供素材 @Override protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { SimpleAuthorizationInfo info =new SimpleAuthorizationInfo(); User user =(User) principals.getPrimaryPrincipal(); List<String> list =userService.findModuleName(user.getUserId()); info.addStringPermissions(list); return info; }
2. 在jsp頁面中經過shiro標籤對頁面進行判斷處理 例如: 對這幾個模塊進行權限控制判斷架構
1) 添加shiro的@taglib頭文件 < %@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%> 2)對須要進行權限判的模塊使用shiro標籤進行處理 <div id="menuContent"> <shiro:authenticated name="系統管理"> <span id="topmenu" onclick="toModule('home');">系統首頁</span><span id="tm_separator"></span> </shiro:authenticated> <shiro:authenticated name="貨運管理"> <span id="topmenu" onclick="toModule('cargo');">貨運管理</span><span id="tm_separator"></span> </shiro:authenticated> <shiro:authenticated name="基礎信息"> <span id="topmenu" onclick="toModule('baseinfo');">基礎信息</span><span id="tm_separator"></span> </shiro:authenticated> <shiro:authenticated name="系統管理"> <span id="topmenu" onclick="toModule('sysadmin');">系統管理</span> </shiro:authenticated> </div>