2024.5.24 Friday
接上文【WEEK13】 【DAY4】Shiro第四部分【中文版】
目录
- 15.7.Shiro请求授权的实现
- 15.7.1.修改ShiroConfig.java
- 15.7.1.1.添加一行验证授权的代码
- 15.7.1.2.重启
- 15.7.2.修改MyController.java
- 15.7.3.修改ShiroConfig.java
- 15.7.4.重启
- 15.7.5.修改UserRealm.java
- 15.7.5.1.给所有用户增加权限
- 15.7.5.2.重启验证
15.7.Shiro请求授权的实现
15.7.1.修改ShiroConfig.java
15.7.1.1.添加一行验证授权的代码
//授权。正常情况下,应该跳转到未授权页面,但是此时由于只添加了以下的验证,导致直接跳转到401页面
filterMap.put("/user/add","perms[user:add]");
15.7.1.2.重启
任选一用户登录,尝试访问“add”页面:由于用户权限不足不能访问。
15.7.2.修改MyController.java
package com.P40.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping({"/", "/index"})
public String toIndex(Model model){
model.addAttribute("msg","hello, Shiro");
return "index";
}
@RequestMapping("/user/add")
public String add(){
return "user/add";
}
@RequestMapping("/user/update")
public String update(){
return "user/update";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(String username, String password, Model model){
//获取当前的用户
Subject subject = SecurityUtils.getSubject();
//封装用户的登录数据
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//执行登录的方法,没有异常则成功了,选中代码后Ctrl+Alt+T添加try catch
try {
subject.login(token);
return "index";
} catch (UnknownAccountException e) { //用户名不存在
model.addAttribute("msg","用户名错误");
return "login";
} catch (IncorrectCredentialsException e) { //密码错误
model.addAttribute("msg","密码错误");
return "login";
}
}
@RequestMapping("/noauth")
@ResponseBody
public String unauthorized(){
return "未授权,禁止访问此页面";
}
}
15.7.3.修改ShiroConfig.java
添加一个未授权页面的跳转
package com.P40.config;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//要配置什么:点击进入ShiroFilterFactoryBean源码查看
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
//添加shiro的内置过滤器
/*
anon: 无需认证就可以访问
authc: 必须认证了才能访问
user: 必须拥有记住我功能才能用
perms: 拥有对某个资源的权限才能访问
role: 拥有某个角色权限
*/
//登录拦截
Map<String,String> filterMap = new LinkedHashMap<>();
//授权。正常情况下,应该跳转到未授权页面,但是此时由于只添加了以下的验证,导致直接跳转到401页面
filterMap.put("/user/add","perms[user:add]");
// filterMap.put("/user/add","authc");
// filterMap.put("/user/update","authc");
//仅在此处修改add和update页面的访问权限后,重启项目,点击add或update都将被拦截,显示404错误,希望跳转到登录页面
filterMap.put("/user/*","authc"); //也可以使用通配符实现(替代上两行的/user/add和/user/update)
bean.setFilterChainDefinitionMap(filterMap);
//如果没有权限,则需要跳转到登录页
bean.setLoginUrl("/toLogin"); //设置登录的请求
//未授权页面
bean.setUnauthorizedUrl("/noauth");
return bean;
}
//DefaultWebSecurityManager
@Bean(name = "securityManager") //给这个类起别名,便于ShiroFilterFactoryBean调用
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){ //获取UserRealm,但是这里好像不需要使用注解,可以直接调用
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//默认的DefaultWebSecurityManager类的类名是defaultWebSecurityManager,只是在这里修改成securityManager
//关联UserRealm
securityManager.setRealm(userRealm);
return securityManager;
}
//创建realm对象,需要自定义类
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
//创建时顺序是相反的(从real->DefaultWebSecurityManager->ShiroFilterFactoryBean)
}
15.7.4.重启
同样是在没有访问add页面的访问权限时尝试访问该页面,此时显示的url和相应的页面如下:
15.7.5.修改UserRealm.java
15.7.5.1.给所有用户增加权限
package com.P40.config;
import com.P40.pojo.User;
import com.P40.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
//UserRealm是一个bean
//自定义的UserRealm,必须要继承AuthorizingRealm方法,然后implement methods(alt+insert)
public class UserRealm extends AuthorizingRealm {
@Autowired
UserService userService;
//授权authorization
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("do doGetAuthorizationInfo 授权");
//authorization
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //可以先写new SimpleAuthorizationInfo();然后使用Alt+Enter快捷创建前面的框架
info.addStringPermission("user:add"); //给所有登录的用户增加访问User:add的权限,此时登录任一用户都能访问add页面
//不能return null
return info;
}
//认证authentication
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("do doGetAuthenticationInfo 认证");
UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
//改为连接真实数据库
User user = userService.queryUserByName(userToken.getUsername());
if(user == null){ //此用户不存在
return null; //UnknownAccountException
}
//密码认证,shiro运行
return new SimpleAuthenticationInfo("",user.getPwd(),"");
}
}
15.7.5.2.重启验证
Shiro请求授权的实现 这部分内容还剩一半,详见【WEEK14】 【DAY1】Shiro第六部分【中文版】