Spring Boot มีกรอบการตรวจสอบความถูกต้องในตัวและยังให้การขยายการกำหนดค่า Javaconfig ที่กำหนดเอง Spring-Sercurity ยังเป็นกรอบที่ยอดเยี่ยม แต่ฉันคุ้นเคยกับการใช้เฟรมเวิร์ก Apache Shiro ยิ่งไปกว่านั้นโครงการดั้งเดิมยังเป็นกรอบการทำงานของ Shiro แบบบูรณาการ ฉันออนไลน์เพื่อค้นหาวิธีการกำหนดค่า แต่ฉันไม่พบวิธีการกำหนดค่าที่สมบูรณ์ดังนั้นฉันจึงตัดสินใจทำด้วยตัวเองและมีอาหารและเสื้อผ้าเพียงพอ!
ในการรวมกรอบอื่น ๆ ใน Spring Boot คุณต้องเข้าใจวิธี Spring Javaconfig ก่อน คุณยังสามารถกำหนดค่าโมดูลอื่น ๆ โดยใช้วิธีนี้ ไร้สาระน้อยลงเริ่มต้น - -
ก่อนเริ่มต้นคุณต้องนำเข้าการพึ่งพา Maven (Shiro-Web เป็นตัวเลือก):
<การพึ่งพา> <roupId> org.apache.shiro </groupId> <ratifactid> Shiro-core </artifactid> <cersion> $ {Shiro.version} </เวอร์ชัน> </serverency> <Sersion> $ {Shiro.Version} </Sident> </การพึ่งพา> <การพึ่งพา> <roupId> org.apache.shiro </groupId> <ratifactid> Shiro-spring </artifactid> <persion> $ {shiro.version} <ArtIfactId> Shiro-Ehcache </artifactId> <cersion> $ {Shiro.version} </version> </derness>การกำหนดค่าสปริงแบบบูรณาการแบบดั้งเดิมของ Shiro นั้นถูกนำออกไปดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans.xsd" เริ่มต้น </คำอธิบาย> <bean id = "SecurityManager"> <!- แอพ Realm Single หากคุณมีหลายอาณาจักรให้ใช้คุณสมบัติ 'อาณาจักร' แทน -> <property name = "realm" ref = "shirorealMimpl" /> <property name = "cachemanager" ref = "Shiroehcachemanager" /> </ebean> <! ref = "SecurityManager" /> <property name = "loginurl" value = " /login" /> <!-หน้าไม่มีสิทธิ์หรือกระโดดหลังจากล้มเหลว-> <property name = "SuccessUrl" value = " /sa /index" /> <property name = "filterChainDefinitions"> <! /บทบาท/แก้ไข/* = perms [บทบาท: แก้ไข]/บทบาท/บันทึก = perms [บทบาท: แก้ไข]/บทบาท/รายการ = perms [บทบาท: ดู]->/sa/** = authc/** = anon </value> name = "cachemanagerconfigfile" value = "classpath: ehcache-shiro.xml" /> </epean> <!-ตรวจสอบให้แน่ใจว่าการดำเนินการกับการใช้งานวงจรชีวิตภายในของ Shiro นั้นถูกนำมาใช้-> <bean id = "การใช้งาน รันหลังจาก-> <!-LifecyclecleBeanProcessor ได้รัน:-> <ถั่วขึ้นอยู่กับ = "LifecycleBeanPostProcessor"> <property name = "ProxyTargetClass" value = "true"/> </epean>
มีชั้นเรียนมากมายและไม่มีวิธีที่จะกำหนดค่าทีละชั้น ไฟล์ javaconfig มีดังนี้:
นำเข้า java.util.linkedhashmap; นำเข้า java.util.map; นำเข้า org.apache.shiro.cache.ehcache.ehcachemanager; นำเข้า org.apache.shiro.spring.lifecyclebeanpostprocessor; นำเข้า org.apache.shiro.spring.security.interceptor.authorizationattributesourceadvisor; นำเข้า org.apache.shiro.spring.ShirofilterFactoryBean; นำเข้า org.apache.shiro.web.mgt.defaultwebsecurityManager; นำเข้า org.springframework.aop.framework.autoproxy.defaultadvisorautoproxycreator; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; @Configuration คลาสสาธารณะ shiroconfiguration {แผนที่คงที่ส่วนตัว <สตริงสตริง> filterChaInDefinitionMap = ใหม่ linkedHashMap <สตริง, สตริง> (); @bean (name = "ShirorealMimpl") Public ShiRoRealMimpl GetShiROREALM () {ส่งคืน ShiROREALMIMPL () ใหม่ (); } @Bean (name = "ShiroehCacheManager") สาธารณะ ehcachemanager getehcachemanager () {ehcachemanager em = ใหม่ ehcachemanager (); em.setcachemanagerconfigfile ("classpath: ehcache-shiro.xml"); return em; } @Bean (name = "LifeCycleBeanPostProcessor") Public LifecycleBeanPostProcessor GetLifecycleBeanPostProcessor () {ส่งคืน LifecyclecyBeanPostProcessor ใหม่ (); } @Bean Public DefaultAdvisorautoproxyCreator getDefaultAdvisorautoproxycreator () {defaultAdvisorautoproxycreator daap = ใหม่ defaultAdvisorautoproxycreator (); daap.setproxytargetClass (จริง); กลับ Daap; } @Bean (NAME = "SecurityManager") Public DefaultWebsecurityManager GetDefaultWebsecurityManager () {DefaultWebsecurityManager DWSM = ใหม่ defaultWebSecurityManager (); DWSM.SetRealm (getShirorealm ()); DWSM.SetCacheManager (getEhCacheManager ()); กลับ DWSM; } @Bean Public AuthorizationAtTributesourceAdVisor GetAuthorizationAtTributesourceAdVisor () {AuthorizationAtTributesourceAdVisor AASA = ใหม่ AuthorizationAtTributesourceAdVisor (); AASA.SetSecurityManager (getDefaultWebsecurityManager ()); ส่งคืน AuthorizationAttributesourceAdvisor (); } @Bean (name = "Shirofilter") สาธารณะ ShirofilterFactoryBean GetShirofilterFactoryBean () {ShirofilterFactoryBean ShirofilterFactoryBean = ใหม่ ShirofilterFactoryBean (); ShirofilterFactoryBean .SetSecurityManager (getDefaultWebsecurityManager ()); ShirofilterFactoryBean.setLoginurl ("/เข้าสู่ระบบ"); 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 = "10,000" Eternal = "false" timetoidleseconds = "120" timetoLiveSeconds = "120" overflowTodisk = "false"
หมายเหตุ: โปรดดูเอกสารอย่างเป็นทางการสำหรับคลาส shirorealmimpl
สรุป
ด้านบนเป็นวิธีการกำหนดค่าของ spring boot boot shiro ที่แนะนำให้คุณรู้จัก ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!