日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

spring security 登出操作 详细说明

發(fā)布時(shí)間:2024/9/18 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 spring security 登出操作 详细说明 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.前言

這里專門 做 spring security 登出操作 的 詳細(xì)記錄

2.操作

(1)目錄結(jié)構(gòu)

(2)在security 攔截規(guī)則配置文件添加退出登錄支持

源碼

package com.example.security5500.securityConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

//@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DbUserDetailsService dbUserDetailsService;


    //攔截規(guī)則設(shè)置
    @Override
    protected void  configure(HttpSecurity http) throws Exception {
//        http
//        .authorizeRequests()
//        // 匹配 "/" 路徑,不需要權(quán)限即可訪問
//        .antMatchers("/").permitAll()
//        //匹配 "/user" 及其以下所有路徑,都需要 "USER" 權(quán)限
////        .antMatchers("/user/**").hasAuthority("USER")
//        .and()
//        //登錄地址為 "/login",登錄成功默認(rèn)跳轉(zhuǎn)到頁(yè)面 "/hai"
//        .formLogin().loginPage("/login").defaultSuccessUrl("/hai")
//        //退出登錄的地址為 "/logout",退出成功后跳轉(zhuǎn)到頁(yè)面 "/login"
//        .and()
//        //退出登錄的地址為 "/logout",退出成功后跳轉(zhuǎn)到頁(yè)面 "/login"
//        .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
//        // 默認(rèn)啟用 CSRF  ,必須post才可以訪問/logout
        http
                //允許基于使用HttpServletRequest限制訪問
                .authorizeRequests()
                //設(shè)置不攔截頁(yè)面,可直接通過(guò),路徑訪問 "/", "/index", "/home" 則不攔截,
        //"/hhk/**" 的意思是 "/hhk" 及其以下所有路徑
                .antMatchers("/", "/index", "/home","/hhk/**")
                //是允許所有的意思
                .permitAll()
                //其他頁(yè)面都要攔截,【需要在最后設(shè)置這個(gè)】
                .anyRequest().authenticated()
                .and()
                //設(shè)置自定義登錄頁(yè)面
                .formLogin()
                //指定自定義登錄頁(yè)面的訪問虛擬路徑
                .loginPage("/login")
                .permitAll()
                .and()
//        添加退出登錄支持。當(dāng)使用WebSecurityConfigurerAdapter時(shí),這將自動(dòng)應(yīng)用。默認(rèn)情況是,訪問URL”/ logout”,使HTTP Session無(wú)效
//        來(lái)清除用戶,清除已配置的任何#rememberMe()身份驗(yàn)證,清除SecurityContextHolder,然后重定向到”/login?success”
                .logout()
//                //指定的登出操作的虛擬路徑,需要以post方式請(qǐng)求這個(gè) http://localhost:5500/mylogout 才可以登出 ,也可以直接清除用戶認(rèn)證信息達(dá)到登出目的
                .logoutUrl("/mylogout")
        //登出成功后訪問的地址
                .logoutSuccessUrl("/home");
//                .permitAll();
    }


    /**
     * 添加 UserDetailsService, 實(shí)現(xiàn)自定義登錄校驗(yàn)
     */
    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {
        //注入用戶信息,每次登錄都會(huì)來(lái)這查詢一次信息,因此不建議每次都向mysql查詢,應(yīng)該使用redis
        builder.userDetailsService(dbUserDetailsService);
    }

    /**
     * 密碼加密
     */
    @Bean
    public static PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }


}

View Code

(3)前端以表單的形式登出

3.測(cè)試

啟動(dòng)后,

直接 點(diǎn)擊 登出即可

如果把不配置security 的攔截規(guī)則 ,將會(huì)默認(rèn)使用get 方式 請(qǐng)求 /logout 來(lái)登出 ,會(huì)進(jìn)入一個(gè)界面 ,需要鼠標(biāo)點(diǎn)擊

4.后端自定義登出

package com.example.security5500.controller;


import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {

    //登出操作
    @RequestMapping({"/doLogout"})
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {//清除認(rèn)證
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        //重定向到指定頁(yè)面
        return "redirect:/login";
    }

}

請(qǐng)求這個(gè)也可以登出 ,原理是獲取授權(quán)對(duì)象然后清除認(rèn)證,再重定向到指定頁(yè)面

總結(jié)

以上是生活随笔為你收集整理的spring security 登出操作 详细说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。