Spring Boot fournit un cadre d'authentification intégré et fournit également des extensions de configuration Javaconfig personnalisées. Spring-Servity est également un excellent cadre, mais j'ai l'habitude d'utiliser le framework Apache Shiro. De plus, le projet original était un cadre de Shiro intégré. Je suis allé en ligne pour trouver la méthode de configuration, mais je n'ai pas pu trouver de méthode de configuration complète, j'ai donc décidé de le faire moi-même et d'avoir suffisamment de nourriture et de vêtements!
Pour intégrer d'autres frameworks sur Spring Boot, vous devez d'abord comprendre la méthode Spring Javaconfig. Vous pouvez également configurer d'autres modules à l'aide de cette méthode. Moins non-sens, commencez. . .
Avant de commencer, vous devez importer des dépendances Maven (Shiro-Web en option):
<dependency> <proupId> org.apache.shiro </prôdId> <Artifactid> shiro-core </retifactid> <version> $ {shiro.version} </-version> </dependency> <dependency> <proupid> org.apache.shiro </proupId> <Artifactid> Shiro-web </letifactid> <version> $ {shiro.version} </ version> </ Dependency> <Dedency> <ProupId> org.apache.shiro </rom groupe> <artifactive> shiro-spring </refactive> <version> $ {shiro.version} </ version> </pedency> <dependance> <proupId> Org.apache.shiro </pedency> <ArtefactId> Shiro-ehcache </letefactive> <DERNÉRATION> $ {shiro.version} </DERNIERS> </DENDENCENCE>La configuration de ressort intégrée de Shiro d'origine est supprimée, comme suit:
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" XSI: ScheMalation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans.xsd" Default-lazy-init = "true"> <description> SHIRO SECURITÉ CONFIGURATION DE: HTTP:///shiro.apache.oro Security. </ Description> <bean id = "SecurityManager"> <! - Application de royaume unique. Si vous avez plusieurs royaumes, utilisez plutôt la propriété «Realms». -> <propriété name = "royaume" ref = "shirorealmimpl" /> <propriété name = "cacheManager" ref = "shiroehcachemanager" /> </ bean> <! - Définissez le royaume que vous souhaitez utiliser pour vous connecter à votre dataSource de sécurité back-end: -> <bean id = "proboresser" Ref = "SecurityManager" /> <propriété name = "Loginurl" value = "/ login" /> <! - Page qui n'a pas l'autorisation ou les sauts après l'échec -> <propriété name = "SuccessUrl" value = "/ sa / index" /> <propriété Name = "FilterChainDefinitions"> <! - Rôles [admin], perms [document: Read] -> <value> <! / Role / Edit / * = perms [Role: Edit] / Role / Save = PERMS [Role: Edit] / Role / List = PERMS [Role: View] -> / SA / ** = Authc / ** = Anon </ Value> </ Property> </ Bean> <! - User Authorization / Authentication Information Cache, <pèbre Cache -> <Bean Id = "ShiroEHCACHEMAGER" name = "CacheManagerConfigfile" value = "classpath: ehcache-shiro.xml" /> </ank> <! - Assurez-vous que l'exécution de bean de la fonction de cycle de vie interne de Shiro est implémentée -> <bean id = "LifecycleByPostProcessor" /> <! - AOP la méthode de la méthode -> <! Exécuter uniquement après -> <! - Le LifecycleBeanProcessor a exécuté: -> <bean defend-on = "lifecycleBeanPostProcessor"> <propriété name = "proxytargetClass" value = "true" /> </ean> </Ean> <propriété name = "SecurityManager" ref = "SecurityManager" /> </Ean> </eans>
Il y a tellement de classes, et il n'y a aucun moyen de les configurer un par un. Le fichier javaconfig est le suivant:
import java.util.linkedhashmap; importation java.util.map; import org.apache.shiro.cache.ehcache.ehcacheManager; import org.apache.shiro.spring.lifecycleBeanPostProcessor; import org.apache.shiro.spring.security.interceptor.authorizationAttributeSourceadVisor; import org.apache.shiro.spring.shirofilterFactoryBean; import org.apache.shiro.web.mgt.defaultwebsecurityManager; import org.springframework.aop.framework.autoproxy.defaultAdvisorAutoproxyCreator; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @Configuration public class shiroconfiguration {private static map <string, string> filterChainDefinitionMap = new LinkedHashMap <String, String> (); @Bean (name = "shirorealMImpl") public shirorealmimpl getShirorealm () {return new shirorealMimpl (); } @Bean (name = "shiroehcachemanager") public ehcachemanager getehcacheManager () {ehcacheManager em = new ehcacheManager (); em.setCacheManagerConfigfile ("classpath: ehcache-shiro.xml"); retourner EM; } @Bean (name = "lifecycleBeanPostProcessor") public lifycleBycleBeanPostProcessor getLifecycleBeanPostProcessor () {return new LifecycleBylePostProcessor (); } @Bean public DefaultAdvisorAutOproxyCreator getDefaultAdvisorAutOproxyCreator () {DefaultAdVisorAutOproxyCreator DAAP = new defaultAdVisorAutOproxyCreator (); daap.setProxyTargetClass (true); retour daap; } @Bean (name = "SecurityManager") public DefaultWebSecurityManager GetDefaultWebSeCurityManager () {DefaultWebSecurityManager dwsm = new defaultWebSecurityManager (); dwsm.setRealm (getshirorealm ()); dwsm.setCacheManager (GetehcacheManager ()); retour dwsm; } @Bean Public AuthorizationAttributeSourceAdvisor GetAuthorizationAtTributesourceAdVisor () {AutorisationAtButeSourceAdVisor aasa = new AuthorizationAtButeSourceCeadVisor (); AASA.SetSecurityManager (GetDefaultWebSecurityManager ()); Renvoie une nouvelle autorisationAtTributesourceadVisor (); } @Bean (name = "shirofilter") public shirofilterfactorybean getshirofilterfactorybean () {shirofilterfactorybean shirofilterfactorybean = new shirofilterfactorybean (); ShirofilterFactoryBean .SetSecurityManager (GetDefaultWebSecurityManager ()); shirofilterFactoryBean.setLoginurl ("/ login"); ShirofilterFactoryBean.SetSuccressurl ("/ sa / index"); filterChainDefinitionMap.put ("/ sa / **", "authc"); filterchainDefinitionMap.put ("/ **", "anon"); ShirofilterFactoryBean .SetFilterChainDefinitionMap (FilterChainDefinitionMap); retour shirofilterfactoryBean; }}Remarque: Le dernier est l'initialisation de FilterChainDefinitionMap. La carte est initialisée par LinkedHashmap. Lorsque vous postulez, configurez-le dans un fichier de propriétés, puis initialisez-le. Après l'avoir réécrit, ce sera OK. Une fois la réécriture, ça devrait être OK.
N'oubliez pas d'aller à ehcache-shiro.xml
<ehcache updatecheck = "false" name = "shiroCache"> <defaultCache maxElementsInMemory = "10000" eternal = "false" TimeToidleConds = "120" timetolivesEconds = "120" overflowtodisk = "false" diskpersistentEL
Remarque: veuillez vous référer à la documentation officielle de la classe ShirorealMimpl
Résumer
Ce qui précède est la méthode de configuration de Spring Boot Integrated Shiro qui vous a été introduit. J'espère que cela vous sera utile. Si vous avez des questions, veuillez me laisser un message et l'éditeur vous répondra à temps. Merci beaucoup pour votre soutien au site Web Wulin.com!