該博客以Web爲基礎java
1、引入依賴shiro-all包含shiro全部的包、shiro-core是核心包、shiro-web是與web整合、shiro-spring是與spring整合、shiro-ehcache是與EHCache整合、shiro-quartz是與任務調度quartz整合等等。這裏咱們只須要引入shiro-spring便可。web
1 https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->2 <dependency>3 <groupId>org.apache.shirogroupId>4 <artifactId>shiro-springartifactId>5 <version>1.5.3version>6 dependency>2、Controller層
@RestController @RequestMapping("/account")public class AccountController {
/** * 登陸 * @param username * @param password * @return */ @PostMapping(path = "/login") public ResultMsg login( @RequestParam(value = "aaccount")String username, @RequestParam(value = "apassword")String pwd, @RequestParam("check") Integer check ) { ResultMsg resultMsg = new ResultMsg(); // 獲取當前用戶 Subject subject = SecurityUtils.getSubject(); /** * 判斷當前用戶是否已經認證過 */ // System.out.println("是否記住我==="+subject.isRemembered()); if (!subject.isAuthenticated()) { // 封裝用戶的登陸數據 UsernamePasswordToken token = new UsernamePasswordToken(username,pwd); Boolean rememberMe = check == 1 ? true:false; System.out.println(rememberMe); token.setRememberMe(rememberMe); //記住我 try { subject.login(token); //登陸認證 resultMsg.setState(200); resultMsg.setMsg("登陸成功"); return resultMsg; } catch (UnknownAccountException u) { System.err.println("用戶不存在"); resultMsg.setState(412); resultMsg.setMsg("用戶不存在"); return resultMsg; } catch (IncorrectCredentialsException i) { System.err.println("密碼錯誤"); resultMsg.setState(412); resultMsg.setMsg("密碼錯誤"); return resultMsg; } catch (LockedAccountException l) { System.err.println("帳戶鎖定"); resultMsg.setState(412); resultMsg.setMsg("帳戶鎖定"); return resultMsg; } } else { resultMsg.setState(403); resultMsg.setMsg("此帳戶已在其餘地方登陸,是否強制下線?"); return resultMsg; } } /** * 退出登陸 * @param num * @return */ @GetMapping(path = "/loginout") public ResultMsg loginOut(Integer num) { ResultMsg resultMsg = new ResultMsg(); // 獲取當前用戶 Subject subject = SecurityUtils.getSubject(); subject.logout();//退出當前登陸 resultMsg.setState(200); if (num == 1) { resultMsg.setMsg("已下線!"); } else { resultMsg.setMsg("當前用戶已退出!"); } return resultMsg; } /** * 未認證返回登陸頁面 * @return */ @GetMapping(path = "/login") public ModelAndView login() { ModelAndView mv = new ModelAndView("/user/login"); return mv; } }Shiro核心配置
ShiroConfig.javaspring
主要建立三大Bean對象數據庫
建議建立順序逆行apache
package com.hk.aefz.shiro.config; @Configurationpublic class ShiroConfig { // ShiroFliterFactoryBean 3 @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean( @Qualifier("securityManager") DefaultWebSecurityManager securityManager ) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();// 設置安全管理器 factoryBean.setSecurityManager(securityManager);// 添加Shiro內置過濾器 /* anon:無需認證就能夠訪問 authc:必須認證才能夠訪問 user:必須擁有記住我功能才能夠訪問 perms:擁有對某個資源的權限才能訪問 role:擁有某個角色權限才能夠訪問 */ Map
繼承AuthorizingRealm類安全
/** * 自定義UserRealm */public class UserRealm extends AuthorizingRealm {// 注入AccountController @Autowired private AccountController accountController; @Autowired private UserInfoService userInfoService; @Autowired private AccountService accountService;// 受權 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.err.println("執行了受權.........");return null; }// 認證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.err.println("執行了認證...........");// 獲取當前登陸帳戶 UsernamePasswordToken accountToken = (UsernamePasswordToken) token; String username = accountToken.getUsername(); // 獲取當前帳號// 鏈接數據庫進行登陸驗證 Account account = accountController.selectByName(username); System.out.println(account); if (account == null) { return null; //拋出 UnknownAccountException 異常 }// 密碼認證 shiro作 存在泄密 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(account, account.getApassword(), account.getAaccount());return info; } }
使用PostMan進行登陸測試app