Spring Boot는 내장 인증 프레임 워크를 제공하며 사용자 정의 된 JavaconFig 구성 확장 기능도 제공합니다. Spring-sercurity는 또한 훌륭한 프레임 워크이지만 Apache Shiro 프레임 워크를 사용하는 데 익숙합니다. 또한 원래 프로젝트는 통합 시로 프레임 워크였습니다. 구성 방법을 찾기 위해 온라인으로 갔지만 완전한 구성 방법을 찾을 수 없었기 때문에 직접 수행하기로 결정하고 충분한 음식과 의류를 가지고 있습니다!
스프링 부팅에서 다른 프레임 워크를 통합하려면 먼저 스프링 Javaconfig 메소드를 이해해야합니다. 이 메소드를 사용하여 다른 모듈을 구성 할 수도 있습니다. 덜 말도 안되는 시작. . .
시작하기 전에 Maven 의존성을 가져와야합니다 (Shiro-Web 옵션) :
<pectionency> <groupid> org.apache.shiro </groupid> <artifactid> shiro-core </artifactid> <버전> $ {shiro.version} </version} </version} </version> <pectionency> <groupid> org.apache.shiro </groupid> shiro-web </artifactid> <버전> $ {shiro.version} </version> </dependency> <pectionency> <groupid> org.apache.shiro </groupid> <artifactid> shiro-spring </artifactid> <버전> $ {shiro.version} </version> </fectionency> <groupid> org.apache.apache.apache.apache <Artifactid> Shiro-ehcache </artifactid> <bersion> $ {shiro.version} </version> </fectionency>원래 Shiro 통합 스프링 구성은 다음과 같이 꺼집니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans.xsd"default-lazy-init = "true"> <descript> Shiro Security Configuration은 http://shiro.org </description> <bean id = "SecurityManager"> <!- 단일 영역 앱. 여러 영역이있는 경우 대신 'Realms'속성을 사용하십시오. -> <property name = "realm"ref = "shirorealmimpl" /> <property name = "cachemanager"ref = "shiroehcachemanager" /> < /bean> <!-백엔드 보안 데이터 소스에 연결하려는 영역을 정의합니다 .-> <bean id = "shirorealmimpl" /> <bean id = "shirofilter" /< "seleal mame"> ref = "SecurityManager" /> <속성 이름 = "loginUrl"value = " /login" /> <!-실패 후 허가 또는 점프가없는 페이지-> <속성 이름 = "successUl"value = " /sa /index" /> <속성 이름 = "FilterCainDefinitions"> <!-, admin : arms [read]-> <! <! /역할/편집/* = perms [역할 : 편집]/역할/저장 = perm [역할 : 편집]/역할/목록 = perms [역할 : view]->/sa/** = authc/** = anon </value> </property> </bean> <!-사용자 인증/인증 정보 캐시, ehcache cache-> <bean id = "shiroehmanager"> 이름 = "CachemanagerConfigfile"value = "classpath : ehcache-shiro.xml" /> < /bean> <!-Shiro의 내부 수명주기 기능의 Bean 실행이 구현되어 있는지 확인하십시오-> <bean id = "Lifecyclebeanpostpocessor" /> <! <! <!-Shiro Shiro method levels. 만 실행-> <!-LifecycleBeanProcessor가 실행되었습니다 :-> <bean eppends-on = "lifecyclebeanpostprocessor"> <property name = "proxytargetClass"value = "true"/> </bean> <bean> <속성 이름 = "securitymanager"ref = "securityManager"/> </beans>
클래스가 너무 많아서 하나씩 구성 할 방법이 없습니다. javaconfig 파일은 다음과 같습니다.
java.util.linkedhashmap import; java.util.map import; 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; org.springframework.aop.framework.autoproxy.defaultAdvisorAutoProxycreator; import org.springframework.context.annotation.bean; org.springframework.context.annotation.configuration; @configuration public class shiroconfiguration {private static map <string, String> filterchainDefinitionMap = new LinkedHashMap <String, String> (); @Bean (이름 = "shirorealmimpl") public shirorealmimpl getshirorealm () {return new shirorealmimpl (); } @Bean (name = "shiroehcachemanager") public ehcachemanager getehcachemanager () {ehcachemanager em = new ehcachemanager (); Em.setCachemanagerConfigfile ( "classpath : ehcache-shiro.xml"); 반환 EM; } @Bean (이름 = "LifeCycleBeanPostProcessor") public LifeCycleBeanPostProcessor getLifeCycleBeanPostProcessor () {return new LifeCycleBeanPostProcessor (); } @Bean public defaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator () {DefaultAdvisorAutoProxyCreator DAAP = 새로운 DefaultAdvisorAutoProxyCreator (); DAAP.SETPROXYTARGETCLASS (true); 반환 DAAP; } @Bean (name = "securityManager") public defaultWebSecurityManager getDefaultWebSecurityManager () {defaultWebSecurityManager DWSM = New DefaultWebSecurityManager (); dwsm.setRealm (getshirorealm ()); dwsm.setCacheManager (getehcachemanager ()); 반환 DWSM; } @Bean public authorizationAttributesOURCEADvisor getAuthorizationAttributesOUrceAdvisor () {authorizationAttributesourceadvisor aasa = new AuthorizationAttributesOURCeadvisor (); AASA.SetSecurityManager (getDefaultWebsecurityManager ()); 새로운 권한 AuthorizationAttributesourceadvisor ()를 반환합니다. } @Bean (name = "shirofilter") public shirofilterfactorybean getshirofilterfactorybean () {shirofilterfactorybean shirofilterfactorybean = new shirofilterfactorybean (); shirofilterFactoryBean .SetSecurityManager (getDefaultWebSecurityManager ()); shirofilterfactorybean.setLoginUrl ( "/login"); shirofilterfactorybean.setsuccessurl ( "/sa/index"); FilterChainDefinitionMap.put ( "/sa/**", "authc"); FilterChainDefinitionMap.put ( "/**", "anon"); shirofilterFactoryBean .SetFilterChainDefinitionMap (FilterChainDefinitionMap); 반환 shirofilterfactorybean; }}참고 : 마지막은 FilterChainDefinitionMap의 초기화입니다. 지도는 LinkedHashMap에 의해 초기화됩니다. 적용하면 속성 파일로 구성한 다음 초기화하십시오. 다시 쓰면 괜찮을 것입니다. 다시 쓰면 괜찮을 것입니다.
ehcache-shiro.xml로가는 것을 잊지 마십시오
<ehcache updateCheck = "false"name = "shirocache"> <defaultCache maxelementsInmemory = "10000"Eternal = "false"timetoidleseconds = "120"timetoliveseconds = "120"OverFlowTodisk = "false"diskpersistent = "false"iskexxpurythreadintervalseconds = "120" /> / / / / />
참고 : Shirorealmimpl 클래스의 공식 문서를 참조하십시오.
요약
위는 Spring Boot 통합 Shiro의 구성 방법이 소개되었습니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!