1.添加依賴包:java
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency>
2.添加shiro.ini配置(放在src目錄下,mavan環境放置在src/main/resource目錄下)web
[main] #realm #自定義Realm myRealm = com.jsaas.core.security.ShiroDbRealm securityManager.realm = $myRealm #配置shiro的密碼驗證方式爲鹽加密 也能夠經過ShiroDbRealm 中 setCredentialsMatcher方法指定自定義的密碼驗證方式 credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher credentialsMatcher.hashAlgorithmName=SHA-1 credentialsMatcher.hashIterations=1024 credentialsMatcher.storedCredentialsHexEncoded=true myRealm.credentialsMatcher=$credentialsMatcher #沒有登陸的用戶請求須要登陸的頁面時自動跳轉到登陸頁面,不是必須的屬性,不輸入地址的話會自動尋找項目web項目的根目錄下的」/login.jsp」 shiro.loginUrl = /tologin #登陸成功默認跳轉頁面,不配置則跳轉至」/」。若是登錄前點擊的一個須要登陸的頁面,則在登陸自動跳轉到那個須要登陸的頁面。不跳轉到此。 shiro.successUrl = /sys/user/successUrl #沒有權限默認跳轉的頁面。 shiro.unauthorizedUrl = /403 #cache shiroCacheManager = org.apache.shiro.cache.ehcache.EhCacheManager shiroCacheManager.cacheManagerConfigFile = classpath:ehcache.xml securityManager.cacheManager = $shiroCacheManager #session #sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO sessionDAO = com.jsaas.core.security.OnlineSessionDao sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager sessionDAO.activeSessionsCacheName = shiro-activeSessionCache sessionManager.sessionDAO = $sessionDAO securityManager.sessionManager = $sessionManager securityManager.sessionManager.globalSessionTimeout = 360000 [urls] /login/** = anon /user/** = anon /** = authc
配置web.xml文件spring
<!-- 配置apache shiro監聽 --> <listener> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> </listener> <!-- 配置apache shiro過濾器 --> <filter> <filter-name>ShiroFilter</filter-name> <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class> </filter>
3.ShiroDbRealm類sql
package com.jsaas.core.security; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.credential.CredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; import com.jfinal.kit.Kv; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.SqlPara; import com.jsaas.model.User; import com.jsaas.utils.Encodes; import com.jsaas.utils.MyUtils; /** * @Title: ShiroDbRealm.java * @Package com.jsaas.core.security * @Description: TODO(shiro) * @author tuozq * @date 2017年11月3日 下午4:37:20 * @version V1.0 */ public class ShiroDbRealm extends AuthorizingRealm { /*@Override public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) { // TODO Auto-generated method stub //自定義密碼驗證類 集成SimpleCredentialsMatcher 實現doCredentialsMatch方法 super.setCredentialsMatcher(new MyCredentialsMatcher()); }*/ /** * 登陸認證 * 身份認證 * SecurityUtils.getSubject().login(token) 時調用此方法 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // TODO Auto-generated method stub CaptchaUsernamePasswordToken authcToken = (CaptchaUsernamePasswordToken) token; if (authcToken.getUsername()==null||StrKit.isBlank(authcToken.getUsername())) { throw new AuthenticationException("用戶名不能夠爲空"); } String account = authcToken.getUsername(); SqlPara sqlPara = Db.getSqlPara("user.findUser", Kv.by("account", account)); User user = User.dao.findFirst(sqlPara); if(MyUtils.isNotNull(user)){ byte[] salt = Encodes.decodeHex(user.getSalt()); //UserPrincipal爲自定義用戶身份信息,登陸成功後能夠經過SecurityUtils.getSubject().getPrincipal()獲取身份信息 return new SimpleAuthenticationInfo(new UserPrincipal(user), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } return null; } /** * 此方法調用 hasRole,hasPermission的時候纔會進行回調. * * 權限信息.(受權): * 一、若是用戶正常退出,緩存自動清空; * 二、若是用戶非正常退出,緩存自動清空; * 三、若是咱們修改了用戶的權限,而用戶不退出系統,修改的權限沒法當即生效。 * :Authorization 是受權訪問控制,用於對用戶進行的操做受權,證實該用戶是否容許進行當前操做,如訪問某個連接,某個資源文件等。 * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { // TODO Auto-generated method stub return null; } }
4.註冊時用戶的密碼須要進行SHA-1鹽加密apache
public User sha1Password(User user){ //隨機數 + 用戶帳號做爲salt值 String salt = new SecureRandomNumberGenerator().nextBytes().toHex() + user.getAccount(); // 對密碼加鹽進行1024次SHA1加密 String _password = new SimpleHash("SHA-1", user.getPassword(), salt, 1024).toHex(); user.setSalt(salt); //經過鹽值加密密碼 user.setPassword(_password); return user; }