สถานการณ์แอปพลิเคชัน
เมื่อเร็ว ๆ นี้มีคนในชุมชนที่โพสต์บทความด้วยโฆษณาขนาดเล็กซึ่งส่งผลกระทบต่อบรรยากาศของชุมชนอย่างจริงจัง ฉันโกรธมาก! สำหรับผู้ใช้ประเภทนี้ควรถูกบล็อกอย่างถาวร!
กรอบความปลอดภัยของชุมชนใช้ความปลอดภัยในฤดูใบไม้ผลิและฤดูใบไม้ผลิและสถานะการเข้าสู่ระบบนั้นใช้ได้เป็นเวลา 30 วัน ข้อมูลเซสชันมีอยู่ใน Redis จะจัดการกับผู้ใช้ที่ไม่ซื่อสัตย์เหล่านี้ได้อย่างไร?
ก่อนอื่นเพียงแบ่งสิทธิ์ของผู้ใช้:
จากนั้นบล็อกผู้ใช้ที่ระบุ (role_user -> role_black) และบังคับให้ผู้ใช้ออก (ลบข้อมูลเซสชันของผู้ใช้ใน Redis)
การพึ่งพาและการกำหนดค่าที่เกี่ยวข้องกับโครงการ
การพึ่งพา maven
<!-ความปลอดภัย-> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Security </artifactid> </การพึ่งพา <ArtIfactId> Spring-Boot-Starter-Data-Redis </artifactid> </perdency> <!-ฤดูใบไม้ผลิเซสชัน redis-> <การพึ่งพาอาศัย> <GroupId> org.springframework.session </groupid>
แอปพลิเคชันการกำหนดค่านโยบายของฤดูใบไม้ผลิแอปพลิเคชัน
# ละเว้นการเชื่อมต่อที่เกี่ยวข้องกับการเชื่อมต่อ Redis ที่นี่ฤดูใบไม้ผลิ: เซสชัน: Store-type: Redis
ตัวอย่างรหัสการกำหนดค่าความปลอดภัยของสปริง
@enableWebsecurityPublic คลาส WebSecurityConfig ขยาย WebSecurityConfigurerAdapter {@Override Void Proftected Void Configure (httpsecurity http) โยนข้อยกเว้น {http .authorizeRequests () .AntMatchers ("/ผู้จัดการ/**"). hasanyrole (roleenum.Manager.getMessage ()) .anyRequest (). permitall () .and (). formlogin (). loginPage ("/เข้าสู่ระบบ"). และ logout (). -บังคับให้ออกจากส่วนต่อประสานกับผู้ใช้
นำเข้า com.spring4all.bean.responsebean; นำเข้า com.spring4all.service.userservice; นำเข้า lombok.allargsconstructor; นำเข้า org.springframework.session.findbyindexnamesessionrepository; org.springframework.session.data.redis.redisoperationssessionrepository; นำเข้า org.springframework.web.bind.annotation.getmapping; นำเข้า org.springframework.web.bind.annotation.pathvariable; org.springframework.web.bind.annotation.restcontroller; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า java.util.map;@restcontroller@allargsconstructorpublic ขยายเซสชัน> SessionRepository; ส่วนตัวครั้งสุดท้าย redisoperationssessionrepository redisoperationssessionrepository; ผู้ใช้บริการผู้ใช้บริการขั้นสุดท้ายส่วนตัว; /*** จัดการการออกจากระบบของผู้ใช้ที่ระบุ* @param user user user id* @return ข้อมูลเซสชันผู้ใช้*/@preauthorize ("hasrole ('manager')") @getMapping ("/manager/logout/{userid}") ข้อมูลการตอบสนองสาธารณะ หากต้องการใช้ String indexname = userservice.getPrincipalNameIndexName (userId); // สอบถามข้อมูลเซสชันของผู้ใช้ปุ่ม Return Value คือ Map SessionId <String,? ขยายเซสชัน> usersessions = sessionRepository.FindByIndexNameAndIndExValue (findByIndExNamesessionRepository.principal_name_index_name, indexname); // ลบรายการข้อมูลเซสชันของผู้ใช้ <String> sessionIds = new ArrayList <> (usersessions.keyset ()); สำหรับ (String Session: SessionIds) {redisoperationsSessionRepository.deleteById (เซสชัน); } return responsebean.success (ผู้ใช้); - คำอธิบาย indexname คือค่าส่งคืนของ principal.getName ()
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น