shiro的配置主要集中在 ShiroFilterFactoryBean 中app
關於權限:ide
anon:無需認證就能夠訪問post
authc:必須認證了才能訪問 測試
user:必須用有了 記住我 功能才能用 url
perms:擁有對某個資源的權限才能訪問 spa
role:擁有某個角色權限才能訪問code
ShiroConfig 中 ShiroFilterFactoryBeanorm
給控制器添加2個 "必須認證了才能訪問"xml
也就是使用 authc
@Bean(name = "shiroFilterFactoryBean") public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")DefaultWebSecurityManager defaultWebSecurityManager){ ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean(); bean.setSecurityManager(defaultWebSecurityManager); /* * anon:無需認證就能夠訪問 * authc:必須認證了才能訪問 * user:必須用有了 記住我 功能才能用 * perms:擁有對某個資源的權限才能訪問 * role:擁有某個角色權限才能訪問 */ Map<String ,String> filterMap = new LinkedHashMap<>(); filterMap.put("/user/add","authc"); filterMap.put("/user/update","authc"); bean.setFilterChainDefinitionMap(filterMap); return bean; }
訪問測試
點擊 add 和 update
都出現這個404頁面
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>登陸</h1> <hr> <form action=""> <p>用戶名:<input type="text" name="username"></p> <p>密碼:<input type="text" name="password"></p> <p><input type="submit"></p> </form> </body> </html>
@RequestMapping("/toLogin") public String toLogin(){ return "login"; }
1.add 與 update 跳轉合併
filterMap.put("/user/*","authc");
2.添加一個登陸跳轉
bean.setLoginUrl("/toLogin");
@Bean(name = "shiroFilterFactoryBean") public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")DefaultWebSecurityManager defaultWebSecurityManager){ ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean(); bean.setSecurityManager(defaultWebSecurityManager); /* * anon:無需認證就能夠訪問 * authc:必須認證了才能訪問 * user:必須用有了 記住我 功能才能用 * perms:擁有對某個資源的權限才能訪問 * role:擁有某個角色權限才能訪問 */ Map<String ,String> filterMap = new LinkedHashMap<>(); filterMap.put("/user/*","authc"); bean.setFilterChainDefinitionMap(filterMap); bean.setLoginUrl("/toLogin"); return bean; }
點擊 add 和 update
出現登陸頁面
1.獲取當前的用戶
Subject subject = SecurityUtils.getSubject();
2.封裝用戶的登陸數據
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
3.執行登陸方法,若是沒有異常說明OK
subject.login(token);
4.對應異常
UnknownAccountException:用戶名不存在
IncorrectCredentialsException:密碼錯誤
@RequestMapping("/login") public String login(String username, String password, Model model) { //獲取當前的用戶 Subject subject = SecurityUtils.getSubject(); //封裝用戶的登陸數據 UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); //執行登陸方法,若是沒有異常說明OK return "index"; } catch (UnknownAccountException e) { //用戶名不存在 model.addAttribute("msg", "用戶名錯誤"); return "login"; } catch (IncorrectCredentialsException e) { //密碼錯誤 model.addAttribute("msg", "密碼錯誤"); return "login"; } }
UserRealm 中 doGetAuthenticationInfo
先將數據寫死測試
只需判斷用戶名,密碼shiro判斷
//認證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { System.out.println("執行了=>認證doGetAuthenticationInfo"); //用戶名+密碼 String name = "root"; String password="123"; UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; if (!token.getUsername().equals(name)){ return null; //return null 會自動拋出異常 } //密碼認證,shiro作 return new SimpleAuthenticationInfo("",password,""); }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>登陸</h1> <hr> <form th:action="@{/login}"> <p>用戶名:<input type="text" name="username"></p> <p>密碼:<input type="text" name="password"></p> <p><input type="submit"></p> </form> <p th:text="${msg}" style="color: red"></p> </body> </html>