
- 搭建一个springboot 项目
- 引入依赖,主要是web starter shiro(安全认证) 和thymeleaf(简单的页面构建),这里不做数据层的查询,所以不引入数据库相关的。
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-thymeleaforg.apache.shiro shiro-spring-boot-starter1.5.3
- 页面的构建,目录如下:
首页入口
helloword.html
首页
首页
add | update
点击add/update 接口的调用,将在这两个接口加入认证
add.html
添加
update.html
更新
点击接口如果没有登录,将跳转到登录页面
login/loginuser.html
登录
登录
- 写一个controller,给页面提供接口
@Controller
@RequestMapping("/login")
public class LoginController {
@GetMapping("/index")
public String gotoIndex(Model model) {
model.addAttribute("msg","hello shiro");
return "helloword";
}
@GetMapping("/add")
public String add(Model model, HttpServletRequest request) {
model.addAttribute("msg","添加成功!!!");
HttpSession session = request.getSession();
Object user_session = session.getAttribute("USER_SESSION");
return "add";
}
@GetMapping("/update")
public String update(Model model) {
model.addAttribute("msg","更新成功!!!");
return "update";
}
@GetMapping("/tologin")
public String tologin(Model model) {
return "login/loginuser";
}
}
shiro讲解
在概念层,Shiro 架构包含三个主要的理念:Subject,SecurityManager和 Realm。下面的图展示了这些组件如何相互作用,我们将在下面依次对其进行描述。
-
Subject:当前用户,Subject
可以是一个人,但也可以是第三方服务、守护进程帐户、时钟守护任务或者其它–当前和软件交互的任何事件。 -
SecurityManager:管理所有Subject,SecurityManager 是 Shiro
架构的核心,配合内部安全组件共同组成安全伞。 Realms:用于进行权限信息的验证,我们自己实现。Realm 本质上是一个特定的安全 -
Realm:它封装与数据源连接的细节,得到Shiro 所需的相关的数据。在配置 Shiro
的时候,你必须指定至少一个Realm来实现认证(authentication)和/或授权(authorization)。
首先要有一个配置类 ShiroConfig,里面提供一些bean,ShiroFilterFactoryBean,DefaultWebSecurityManager,Realm;我们先搭建一个Realm,这个类主要做认证和授权
public class MyRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了授权");
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行了认证");
String name = (String)authenticationToken.getPrincipal();
System.out.println("用户:"+name);
//模拟从数据库中获取用户登录信息
String username = "sb";
String password = "123456";
if (!username.equals(name)){
return null;
}
return new SimpleAuthenticationInfo(name,password,"");
}
}
ShiroConfig
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean
@Bean()
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
Map filterChainDefinitionMap = new linkedHashMap();
//不拦截的接口
filterChainDefinitionMap.put("/login/loginInfo","anon");
filterChainDefinitionMap.put("/login/index","anon");
//需要认证的接口
//filterChainDefinitionMap.put("/login/add","authc");
//filterChainDefinitionMap.put("/login/update","authc");
//其余接口一律拦截
//主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截
filterChainDefinitionMap.put("
public class MyRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了授权");
//获取用户名字
String name = (String) principalCollection.getPrimaryPrincipal();
//使用name从数据库获取用户角色和权限
String role = "admin";
String permission = "test";
//添加角色 权限
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addRole(role);
simpleAuthorizationInfo.addStringPermission(permission);
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行了认证");
String name = (String)authenticationToken.getPrincipal();
System.out.println("用户:"+name);
//模拟从数据库中获取用户登录信息
String username = "sb";
String password = "123456";
if (!username.equals(name)){
return null;
}
//name 可以上面的授权方法获取
//password 用来比对密码
//realmName
return new SimpleAuthenticationInfo(name,password,"");
}
}
然后哪些需要角色或权限的接口上加上注解@RequiresRoles @RequiresPermissions,shiro会自动去比对是否符合访问要求,如下 @RequiresRoles(“admin2”) 是访问不成功的,因为用户绑定了admin(伪代码写死),可以使用@RequiresPermissions(“test:update”)来设置多种权限。
@RequiresRoles("admin2")
@GetMapping("/add")
public String add(Model model, HttpServletRequest request) {
model.addAttribute("msg","添加成功!!!");
HttpSession session = request.getSession();
Object user_session = session.getAttribute("USER_SESSION");
return "add";
}
@RequiresPermissions("test")
@GetMapping("/update")
public String update(Model model) {
model.addAttribute("msg","更新成功!!!");
return "update";
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)