国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法

瀏覽:4日期:2023-09-14 18:52:49

在 Spring Boot 中做權限管理,一般來說,主流的方案是 Spring Security ,但是,僅僅從技術角度來說,也可以使用 Shiro。

Spring Security 和 Shiro 的比較:

Spring Security 是一個重量級的安全管理框架;Shiro 則是一個輕量級的安全管理框架 Spring Security 概念復雜,配置繁瑣;Shiro 概念簡單、配置簡單 Spring Security 功能強大;Shiro 功能簡單 等等

雖然 Shiro 功能簡單,但是也能滿足大部分的業(yè)務場景。所以在傳統(tǒng)的 SSM 項目中,一般來說,可以整合 Shiro。

在 Spring Boot 中,由于 Spring Boot 官方提供了大量的非常方便的開箱即用的 Starter ,當然也提供了 Spring Security 的 Starter ,使得在 Spring Boot 中使用 Spring Security 變得更加容易,甚至只需要添加一個依賴就可以保護所有的接口,所以,如果是 Spring Boot 項目,一般選擇 Spring Security 。

這只是一個建議的組合,單純從技術上來說,無論怎么組合,都是沒有問題的。

在 Spring Boot 中整合 Shiro ,有兩種不同的方案:

第一種就是原封不動的,將 SSM 整合 Shiro 的配置用 Java 重寫一遍。

第二種就是使用 Shiro 官方提供的一個 Starter 來配置,但是,這個 Starter 并沒有簡化多少配置。

原生的整合

創(chuàng)建項目

創(chuàng)建一個 Spring Boot 項目,只需要添加 Web 依賴即可:

Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法

項目創(chuàng)建成功后,加入 Shiro 相關的依賴,完整的 pom.xml 文件中的依賴如下:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency></dependencies>

創(chuàng)建 Realm

接下來我們來自定義核心組件 Realm:

public class MyRealm extends AuthorizingRealm { @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { return null; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); if (!'javaboy'.equals(username)) { throw new UnknownAccountException('賬戶不存在!'); } return new SimpleAuthenticationInfo(username, '123', getName()); }}

在 Realm 中實現(xiàn)簡單的認證操作即可,不做授權,授權的具體寫法和 SSM 中的 Shiro 一樣,不贅述。這里的認證表示用戶名必須是 javaboy ,用戶密碼必須是 123 ,滿足這樣的條件,就能登錄成功!

配置 Shiro

接下來進行 Shiro 的配置:

@Configurationpublic class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean SecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterFactoryBean shiroFilterFactoryBean() { ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); bean.setSecurityManager(securityManager()); bean.setLoginUrl('/login'); bean.setSuccessUrl('/index'); bean.setUnauthorizedUrl('/unauthorizedurl'); Map<String, String> map = new LinkedHashMap<>(); map.put('/doLogin', 'anon'); map.put('/**', 'authc'); bean.setFilterChainDefinitionMap(map); return bean; }}

在這里進行 Shiro 的配置主要配置 3 個 Bean :

首先需要提供一個 Realm 的實例。 需要配置一個 SecurityManager,在 SecurityManager 中配置 Realm。 配置一個 ShiroFilterFactoryBean ,在 ShiroFilterFactoryBean 中指定路徑攔截規(guī)則等。 配置登錄和測試接口。

其中,ShiroFilterFactoryBean 的配置稍微多一些,配置含義如下:

setSecurityManager 表示指定 SecurityManager。 setLoginUrl 表示指定登錄頁面。 setSuccessUrl 表示指定登錄成功頁面。 接下來的 Map 中配置了路徑攔截規(guī)則,注意,要有序。

這些東西都配置完成后,接下來配置登錄 Controller:

@RestControllerpublic class LoginController { @PostMapping('/doLogin') public void doLogin(String username, String password) { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(username, password)); System.out.println('登錄成功!'); } catch (AuthenticationException e) { e.printStackTrace(); System.out.println('登錄失敗!'); } } @GetMapping('/hello') public String hello() { return 'hello'; } @GetMapping('/login') public String login() { return 'please login!'; }}

測試時,首先訪問 /hello 接口,由于未登錄,所以會自動跳轉(zhuǎn)到 /login 接口:

Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法

然后調(diào)用 /doLogin 接口完成登錄:

Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法

再次訪問 /hello 接口,就可以成功訪問了:

Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法

使用 Shiro Starter

上面這種配置方式實際上相當于把 SSM 中的 XML 配置拿到 Spring Boot 中用 Java 代碼重新寫了一遍,除了這種方式之外,我們也可以直接使用 Shiro 官方提供的 Starter 。

創(chuàng)建工程,和上面的一樣

創(chuàng)建成功后,添加 shiro-spring-boot-web-starter ,這個依賴可以代替之前的 shiro-web 和 shiro-spring 兩個依賴,pom.xml 文件如下:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.4.0</version> </dependency></dependencies>創(chuàng)建 Realm

這里的 Realm 和前面的一樣,我就不再贅述。

配置 Shiro 基本信息

接下來在 application.properties 中配置 Shiro 的基本信息:

shiro.sessionManager.sessionIdCookieEnabled=trueshiro.sessionManager.sessionIdUrlRewritingEnabled=trueshiro.unauthorizedUrl=/unauthorizedurlshiro.web.enabled=trueshiro.successUrl=/indexshiro.loginUrl=/login

配置解釋:

第一行表示是否允許將sessionId 放到 cookie 中 第二行表示是否允許將 sessionId 放到 Url 地址攔中 第三行表示訪問未獲授權的頁面時,默認的跳轉(zhuǎn)路徑 第四行表示開啟 shiro 第五行表示登錄成功的跳轉(zhuǎn)頁面 第六行表示登錄頁面

配置 ShiroConfig

@Configurationpublic class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition(); definition.addPathDefinition('/doLogin', 'anon'); definition.addPathDefinition('/**', 'authc'); return definition; }}

這里的配置和前面的比較像,但是不再需要 ShiroFilterFactoryBean 實例了,替代它的是 ShiroFilterChainDefinition ,在這里定義 Shiro 的路徑匹配規(guī)則即可。

這里定義完之后,接下來的登錄接口定義以及測試方法都和前面的一致,我就不再贅述了。大家可以參考上文。

總結(jié)

本文主要向大家介紹了 Spring Boot 整合 Shiro 的兩種方式,一種是傳統(tǒng)方式的 Java 版,另一種則是使用 Shiro 官方提供的 Starter,兩種方式

更多關于Spring Boot技巧請查看下面的相關鏈接

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美成在线 | 久久国产精品自线拍免费 | 欧美日本一道道一区二区三 | 欧美一区二区三区在线观看 | 女人张开腿让男人捅视频 | 欧美国产在线视频 | 波多结衣一区二区三区 | 欧美三级真做在线观看 | 亚洲国产天堂久久精品网 | 国产一级第一级毛片 | 成人免费视频在 | 国产亚洲欧美一区二区三区 | 久久精品久久久久 | 亚洲精品综合一区二区三区 | 韩国免又爽又刺激激情视频 | 欧美一级欧美一级在线播放 | 中文国产成人精品久久水 | 一级特色黄大片 | 色老头久久久久 | 中文字幕在线日韩 | 久99久精品视频免费观看v | 一级毛片黄片 | 久草视频手机在线 | 欧美在线视频免费 | 欧美三级免费网站 | 国产福利一区二区三区 | 18lxxlxx日本| 久久久国产精品网站 | 色老头久久久久 | 久久女同互慰一区二区三区 | 欧美人成在线 | 91成人免费 | 国产精品人成人免费国产 | 中国一级毛片免费观看 | 日本一区二区三区四区无限 | 一级女人毛片 | 国产精品黄在线观看免费软件 | 三级视频在线播放线观看 | 真人一级毛片免费观看视频 | 97干干干| 日本成人不卡视频 |