詳解Java springboot 整合Shiro框架
Shiro介紹
Shiro是一款安全框架,主要的三個(gè)類Subject、SecurityManager、Realm
- Subject:表示當(dāng)前用戶
- SecurityManager:安全管理器,即所有與安全有關(guān)的操作都會(huì)與SecurityManager交互;且其管理著所有Subject;可以看出它是Shiro的核心,它負(fù)責(zé)與Shiro的其他組件進(jìn)行交互,它相當(dāng)于SpringMVC中DispatcherServlet的角色
- Realm:Shiro從Realm 獲取安全數(shù)據(jù)(如用戶、角色、權(quán)限)
Shiro框架結(jié)構(gòu)圖

Springboot整合Shiro
建項(xiàng)目是勾選spring web,導(dǎo)入依賴
<!-- thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.7.1</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.2</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- thymeleaf、shiro整合包-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>編寫頁面及其控制層

轉(zhuǎn)發(fā)的設(shè)置,全部編寫在MVCConfig中的前端控制器中
@Configurationpublic class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/login.html").setViewName("login"); registry.addViewController("/user/add").setViewName("user/add"); registry.addViewController("/user/update").setViewName("user/update"); registry.addViewController("/loginout").setViewName("login"); }}連接數(shù)據(jù)庫
編寫application.yml
spring:
datasource:
username: ***
password: ***
url: jdbc:mysql://localhost:3306/db_2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
type-aliases-package: com.example.demo.pojo
編寫 pojo、dao、service三層,dao層可以直接使Mybatis的注解。
需要的方法就是findByName(String username),通過表單傳入的username值進(jìn)行查詢。
編寫UserRealm 需要繼承AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
@Autowired
private IuserService iuserService;
// 授權(quán)
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("===>授權(quán)");
SimpleAuthorizationInfo Info = new SimpleAuthorizationInfo();
// 獲取登錄對象
Subject subject = SecurityUtils.getSubject();
user principal = (user) subject.getPrincipal();//拿到user
Info.addStringPermission(principal.getPerms());
return Info;
}
// 認(rèn)證
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("==>認(rèn)證");
UsernamePasswordToken authenticationToken1 = (UsernamePasswordToken) authenticationToken;
user byName = iuserService.findByName(authenticationToken1.getUsername());
if(byName==null){
return null;//拋出用戶名錯(cuò)誤的異常
}
//密碼認(rèn)證shiro自己完成 將user對象 傳遞給上面的方法進(jìn)行授權(quán)
return new SimpleAuthenticationInfo(byName,byName.getPassword(),"");
}
}代碼的分析:
認(rèn)證部分:
將表單提交的數(shù)據(jù)封裝成一個(gè)對象,通過username從數(shù)據(jù)庫中查詢返回一個(gè)對象,進(jìn)行比對
最后將這個(gè)查詢的對象傳遞給授權(quán)方法。
授權(quán)部分:
獲取到用戶對象,給用戶對象進(jìn)行相應(yīng)的授權(quán)。(傳遞的user對象中就有權(quán)限設(shè)置)
編寫ShiroConfig
@Configuration
public class ShiroConfig {
@Bean //創(chuàng)建對象
public UserRealm userRealm(){
return new UserRealm();
}
@Bean //接管對象 @Bean 默認(rèn)使用方法名稱
public DefaultWebSecurityManager securityManager(@Qualifier("userRealm") Realm realm){
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
}
@Bean //交給前端處理
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
HashMap<String, String> hashMap = new HashMap<>();
// 該路徑 必須通過認(rèn)證 才能進(jìn)行訪問
hashMap.put("/user/*","authc");
// 進(jìn)行授權(quán)
hashMap.put("/user/add","perms[add]");
hashMap.put("/user/update","perms[update]");
// 注銷
hashMap.put("/logout","logout");
shiroFilterFactoryBean.setFilterChainDefinitionMap(hashMap);
// 設(shè)置登錄頁面的路徑
shiroFilterFactoryBean.setLoginUrl("/login.html");
// 設(shè)置授權(quán)頁面
shiroFilterFactoryBean.setUnauthorizedUrl("/noLogin");
return shiroFilterFactoryBean;
}
// 完成整合
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
}代碼分析
在這個(gè)配置類中,配置的方法就是ioc注入。
ShiroFilterFactoryBean中可以配置
- 資源路徑對應(yīng)的權(quán)限
- 登陸頁面
- 權(quán)限不足 無法訪問的頁面路徑
- 注銷
補(bǔ)充: 攔截的屬性
- anon: 無需認(rèn)證就可以訪問
- authc: 必須認(rèn)證了才能訪問
- user: 必須擁有記住我功能才能用
- perms: 擁有對某個(gè)資源的權(quán)限才能訪問
- role: 擁有某個(gè)角色權(quán)限
編寫控制層代碼
@Controller
public class logincontroller {
// 執(zhí)行流程 前端表單-》控制層代碼-》config
@PostMapping("/login")
public String login(String username, String password, Model model){
// 獲取一個(gè)用戶
Subject subject = SecurityUtils.getSubject();
// 封裝用戶登陸數(shù)據(jù)
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
// 執(zhí)行登錄方法,如果失敗就會(huì)拋出異常
try{
subject.login(usernamePasswordToken);
return "index";
}catch (UnknownAccountException e){
model.addAttribute("msg","用戶名錯(cuò)誤");
return "login";
}catch (IncorrectCredentialsException e){
model.addAttribute("msg","密碼錯(cuò)誤");
return "login";
}
}
@GetMapping("/noLogin")
@ResponseBody
public String nologin(){return "未經(jīng)授權(quán) 無法訪問";}
}代碼分析:
login方法:獲取從表單傳遞的數(shù)據(jù),封裝從UsernamePasswordToken對象,調(diào)用login方法進(jìn)行登錄操作
Shiro整合Thymeleaf
在ShiroConfig需要整合ShiroDialect
// 完成整合
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}約束
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
使用方法
shiro:notAuthenticated:沒有進(jìn)行登錄 顯示shiro:authenticated:已經(jīng)登陸 顯示shiro:hasPermission="A"用戶存在A的權(quán)限則顯示
示例代碼:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首頁</h1>
<div shiro:notAuthenticated>
<a th:href="@{/login.html}">登錄</a>
</div>
<div shiro:authenticated>
<a th:href="@{/logout}">注銷</a>
</div>
<div shiro:hasPermission="add">
<a th:href="@{/user/add}">ADD</a>
</div>
<div shiro:hasPermission="update">
<a th:href="@{/user/update}">UPDATE</a>
</div>
</body>
</html>總結(jié)
登錄的流程:login表單-》loginController-》ShiroConfig-》UserRealm
效果:
點(diǎn)擊登錄,控制臺會(huì)顯示

進(jìn)入add/update的頁面,也會(huì)打印"===>授權(quán)",這個(gè)也證明了登錄的執(zhí)行流程
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java多線程編程之使用thread類創(chuàng)建線程
在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時(shí)需要建立一個(gè)Thread實(shí)例2014-01-01
springboot自動(dòng)配置沒有生效的問題定位(條件斷點(diǎn))
java反射調(diào)用方法NoSuchMethodException的解決方案
MyBatis-Plus攔截器對敏感數(shù)據(jù)實(shí)現(xiàn)加密
一文帶你學(xué)會(huì)規(guī)則引擎Drools的應(yīng)用

