1、Shiro配置的簡要說明。html
<!-- 項目自定義的Realm --> <bean id="shiroDbRealm" class="org.shiro.demo.service.realm.ShiroDbRealm" ></bean> <!-- Shiro Filter --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/" /> <property name="successUrl" value="/system/main" /> <property name="unauthorizedUrl" value="/system/error" /> <property name="filterChainDefinitions"> <value> /login = anon /validateCode = anon /** = authc </value> </property> </bean>
大體解釋:
<bean id="shiroDbRealm" class="org.shiro.demo.service.realm.ShiroDbRealm" ></bean>
這個就是指定Shiro驗證用戶登陸的類爲自定義的ShiroDbRealm.java。java
在Shiro Filter當中:
securityManager:這個屬性是必須的。
loginUrl :沒有登陸的用戶請求須要登陸的頁面時自動跳轉到登陸頁面,不是必須的屬性,不輸入地址的話會自動尋找項目web項目的根目錄下的」/login.jsp」頁面web
successUrl :登陸成功默認跳轉頁面,不配置則跳轉至」/」。若是登錄前點擊的一個須要登陸的頁面,則在登陸自動跳轉到那個須要登陸的頁面。不跳轉到此。
unauthorizedUrl :沒有權限默認跳轉的頁面。
filterChainDefinitions : 就是須要驗證的地址的列表,經常使用的包含anon、authc、perms、roles、user、logout。
/login = anon 表明後綴爲/login的連接不驗證
/** = authc 表明其它後綴的連接都進行登陸驗證,需登陸後才能訪問。spring
2、新建ShiroDbRealm類apache
package org.shiro.demo.service.realm; import javax.annotation.Resource; 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.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.shiro.demo.entity.User; import org.shiro.demo.service.IUserService; public class ShiroDbRealm extends AuthorizingRealm{ @Resource(name="userService") private IUserService userService; protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { return null; } /** * 認證回調函數,登陸時調用. */ protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = userService.getByAccount(token.getUsername()); if (user != null) { return new SimpleAuthenticationInfo(user.getAccount(), user .getPassword(), user.getNickname()); } else { return null; } } }
繼承AuthorizingRealm類,且重寫doGetAuthorizationInfo及doGetAuthenticationInfo方法。
doGetAuthorizationInfo : 驗證當前Subject(可理解爲當前用戶)所擁有的權限,且給其受權。在下一章說明。
doGetAuthenticationInfo : 驗證當前Subject登陸。
userService.getByAccount(token.getUsername());是自定義的方法。app
3、新建UserController.java類jsp
@Controller public class UserController { private static final Log log = LogFactory.getLog(UserController.class); /** * 判斷用戶是否登陸 * @param currUser * @return */ @RequestMapping(value = "/login",method=RequestMethod.POST) public String isLogin(User currUser){ Subject user = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(currUser.getAccount(),currUser.getPassword()); token.setRememberMe(true); try { user.login(token); return "redirect:/main"; }catch (AuthenticationException e) { log.error("登陸失敗錯誤信息:"+e); token.clear(); return "redirect:/login"; } } }
4、新建login.jsp函數
<form action="<%=basePath%>/login" method="post"> 用戶名:<input type="text" name="account"/> <br/> 密碼:<input type="text" name="password"/><br/> <input type="submit" value="登陸" /> </form>
而後經過SpringMVC訪問到login.jsp頁面,測試Shiro的用戶驗證。post