IDEA 快捷键 //ctrl+alt+T 自动弹出try catch
1、导入shiro的jar包
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.7.1</version>
</dependency>
百度搜索 shiro-spring maven
https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring/1.7.1
2、两大核心配置 config
package com.kuang.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;
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean:第3步
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
return bean;
}
//现在就是第一步和第二步绑定起来了 通过@Qualifier("userRealm") UserRealm userRealm
//DefaultWebSecurityManager:第2步
@Bean("securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//关联UserRealm
securityManager.setRealm(userRealm);
return securityManager;
}
//创建 realm 对象 , 需要自定义类:第1步 或者@Bean(name="userRealm") 这个name就是第二步Qualifier处的名字
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
package com.kuang.config;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
//自定义的UserRealm extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了=>授权doGetAuthorizationInfo");
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行了=>认证doGetAuthenticationInfo");
return null;
}
}
加入认证
//添加shiro的内置过滤器
/*
anon:无需认证就可以访问
authc:必须认证了才能访问
user: 必须拥有 记住我 功能才能用
perms:拥有对某个资源的权限才嗯那个访问
role:拥有某个角色权限才能访问
*/
Map<String, String> filerMap = new LinkedHashMap<>();
filerMap.put("/user/add","authc");
filerMap.put("/user/update","authc");
bean.setFilterChainDefinitionMap(filerMap);
登陆拦截
/*
anon:无需认证就可以访问
authc:必须认证了才能访问
user: 必须拥有 记住我 功能才能用
perms:拥有对某个资源的权限才嗯那个访问
role:拥有某个角色权限才能访问
//filerMap.put("/user/add","authc");
//filerMap.put("/user/update","authc");
*/
//登陆拦截
Map<String, String> filerMap = new LinkedHashMap<>();
filerMap.put("/user/*","authc"); //通配符*
bean.setFilterChainDefinitionMap(filerMap);
//设置登录的请求
bean.setLoginUrl("/toLogin");
controller/MyController
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping({"/","/index","/index.html"})
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";
}
}
首页
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<p th:text="${msg}"></p>
<hr>
<a th:href="@{/user/add}">add</a> | <a th:href="@{/user/update}">update</a>
</body>
</html>
登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登陆</h1>
<form action="">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="text" name="password"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
shiro-thymeleaf整合包
https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/2.0.0
<!--shiro-thymeleaf整合包-->
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>