Springboot2.1.6集成activiti7出現(xiàn)登錄驗(yàn)證的實(shí)現(xiàn)
一、問題
Spring2.1.5集成activiti7.1.24時(shí)訪問要輸入用戶名和密碼。
@Autowired private ProcessRuntime processRuntime; /** * 啟動任務(wù) */ @Test void startProcess(){ /** * 流程變量 * 給<userTask id="請假申請" name="請假申請" activiti:assignee="#{student}"></userTask> * 的student賦值 */ HashMap<String, Object> variables = new HashMap<>(); // String username = SecurityUtils.getNickName(); String username = "小王"; variables.put("staff", username); ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder .start() .withProcessDefinitionKey("baoxiao") .withName("報(bào)銷測試") //.withBusinessKey(id) // .withVariable("deptLeader", join) .withVariables(variables) .build()); System.out.println(processInstance.getId()); }
在單元測試中測試Activiti框架,出現(xiàn)如下的異常:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:379)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:223)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:65)
...
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
其余的方式大家如果試過成功就可以了,如果不行,可試下這個(gè):取消登錄驗(yàn)證如下:
@SpringBootApplication( exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class } )
這是我在別人那找來的,困了我好長時(shí)間,知道看到這個(gè)東西。
二、新版驗(yàn)證
其實(shí)這個(gè)都很簡單,但是我看了不少博客下的評論都說按照這個(gè)方式剔除了,但是還是不行
An Authentication object was not found in the SecurityContext
當(dāng)我們自信的對著接口發(fā)起請求的時(shí)候,報(bào)了瀏覽器出現(xiàn)了500,控制臺報(bào)出上面的異常,中文意思:在SecurityContext中沒有找到身份驗(yàn)證對象
why? 我明明已經(jīng)剔除了啊,為什么還是要驗(yàn)證身份?
到底是哪里出了問題?想不明白!
如果上上面的問題,你只需要使用,老的API即可,因?yàn)樾路庋b的API使用SpringSecurity,所以需要身份驗(yàn)證
注意:以下均是個(gè)人理解,如有錯(cuò)誤,還請指正
其實(shí),當(dāng)我們使用Activiti7的時(shí)候,要知道Activit7的開發(fā)團(tuán)隊(duì),不在是之前Tom Baeyens的團(tuán)隊(duì)負(fù)責(zé)開發(fā),也就是下面這個(gè)哥們。
Activiti7中,重新封裝了一些新的API,比如ProcessRuntime和TaskRuntime
既然是新封裝的,那一定和老的API會有所區(qū)別,那具體區(qū)別在哪里呢?
我們可以直接查看這兩個(gè)接口里面的內(nèi)容,去一探究竟!
@Autowired private ProcessRuntime processRuntime;
我這里拿ProcessRuntime舉例,我們可以直接查看下,找到實(shí)現(xiàn)的類
或者直接使用快捷鍵 CTRL + H
,找出實(shí)現(xiàn)的類
進(jìn)入實(shí)現(xiàn)類中:
進(jìn)入實(shí)現(xiàn)類中,我們可以看到類上面使用:
@PreAuthorize("hasRole('ACTIVITI_USER')")
這個(gè)是什么?
SpringSecurity用來在方法調(diào)用前或者調(diào)用后進(jìn)行權(quán)限檢查
說到這里,您應(yīng)該明白了吧!因?yàn)锳ctiviti7中封裝出來的新接口,都加了這個(gè)注解,所以當(dāng)我們即使照著最開始的方法剔除了,仍然還會出現(xiàn)身份認(rèn)證問題!
那為什么我們調(diào)用老的接口,卻不需要身份驗(yàn)證呢,其實(shí)不用開源碼就可以推出,老的接口上并沒有添加該注解
我們可以看看老的接口 RuntimeService
的實(shí)現(xiàn)類:
@Autowired private RuntimeService runtimeService;
所以,我們可以使用 RuntimeService
來代替 ProcessRuntime
啟動實(shí)例。
單元測試實(shí)例:
package com.example.demo; import com.ruoyi.RuoYiApplication; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; @RunWith(SpringRunner.class) @SpringBootTest(classes = RuoYiApplication.class) public class DemoApplicationTests { // Activiti7 新接口由于內(nèi)置SpringSecurity,在運(yùn)行時(shí)會報(bào)錯(cuò) // @Autowired // private ProcessRuntime processRuntime; // 老的接口方法,未內(nèi)置 SpringSecurity @Autowired private RuntimeService runtimeService; @Test void contextLoads() { System.out.println("hello world"); } /** * 在使用 SpringBoot + Activiti 時(shí),啟動服務(wù)訪問模塊時(shí),瀏覽器會彈出一個(gè)登錄界面。 * * 這是因?yàn)锳ctiviti 框架整合了SpringSecurity框架,如果我們不需要安全驗(yàn)證時(shí)可以禁用springsecurity。 * * * @SpringBootApplication( * exclude = { * org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, * org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class * }) */ /** * 啟動任務(wù) */ @Test void startProcess(){ /** * 流程變量 * 給<userTask id="請假申請" name="請假申請" activiti:assignee="#{student}"></userTask> * 的student賦值 */ HashMap<String, Object> variables = new HashMap<>(); // String username = SecurityUtils.getNickName(); String username = "小王"; variables.put("staff", username); /* ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder .start() .withProcessDefinitionKey("baoxiao") .withName("報(bào)銷測試") //.withBusinessKey(id) // .withVariable("deptLeader", join) .withVariables(variables) .build()); */ ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("baoxiao", variables); System.out.println("流程實(shí)例啟動:"); System.out.println(processInstance.getId()); } }
結(jié)果打?。?/p>
流程實(shí)例啟動:
b0af0810-4801-11eb-b1ac-005056c00001
可以看到使用老接口,就可以成功執(zhí)行了。
相關(guān)文章:
SpringBoot2.x整合Activiti7后,禁用SpringBootSecurity問題
到此這篇關(guān)于Springboot2.1.6集成activiti7出現(xiàn)登錄驗(yàn)證的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot activiti7登錄驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis中有關(guān)int和Integer的使用方式
這篇文章主要介紹了MyBatis中有關(guān)int和Integer的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn)
這篇文章主要介紹了Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java使用JDBC連接數(shù)據(jù)庫的實(shí)現(xiàn)方法
這篇文章主要介紹了Java使用JDBC連接數(shù)據(jù)庫的實(shí)現(xiàn)方法,包括了詳細(xì)的加載步驟以及完整實(shí)現(xiàn)示例,需要的朋友可以參考下2014-09-09springboot mybatis調(diào)用多個(gè)數(shù)據(jù)源引發(fā)的錯(cuò)誤問題
這篇文章主要介紹了springboot mybatis調(diào)用多個(gè)數(shù)據(jù)源引發(fā)的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01java客戶端Jedis操作Redis Sentinel 連接池的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava客戶端Jedis操作Redis Sentinel 連接池的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03