欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot?整合Security權(quán)限控制的初步配置

 更新時(shí)間:2022年11月10日 15:50:20   作者:EdurtIO  
這篇文章主要為大家介紹了SpringBoot?整合Security權(quán)限控制的初步配置實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

在源碼目錄下新建 config 目錄, 在該目錄下新建 WebSecurityConfig 類文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
 * WebSecurityConfig <br/>
 * 描述 : WebSecurityConfig <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 創(chuàng)建時(shí)間 : 2018-03-15 下午3:18 <br/>
 * 聯(lián)系作者 : <a href="mailTo:shichengoooo@163.com" rel="external nofollow" >qianmoQ</a>
 */
@Configuration
// 開啟security訪問授權(quán)
@EnableWebSecurity
// 開啟security注解模式
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 允許直接訪問/路徑
        http.authorizeRequests().antMatchers("/").permitAll()
                // 其他路徑需要授權(quán)訪問
                .anyRequest().authenticated()
                // 指定登錄頁面
                .and().formLogin().loginPage("/user/login")
                // 登錄成功后的默認(rèn)路徑
                .defaultSuccessUrl("/").permitAll()
                // 退出登錄后的默認(rèn)路徑
                .and().logout().logoutSuccessUrl("/user/login").permitAll();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // 配置用戶登錄檢索策略
        auth.userDetailsService(userDetailsService())
                // 配置密碼策略
                .passwordEncoder(passwordEncoder());
//        auth.inMemoryAuthentication().withUser("user").password("123456").roles("USER")
//                .and().withUser("admin").password("123456").roles("ADMIN");
    }
    @Bean
    public Md5PasswordEncoder passwordEncoder() {
        return new Md5PasswordEncoder();
    }
    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        // 創(chuàng)建模擬用戶
        manager.createUser(User.withUsername("user").password("123456").roles("USER").build());
        manager.createUser(User.withUsername("admin").password("123456").roles("ADMIN").build());
        return manager;
    }
}

瀏覽器打開 http://localhost:8080/home 會自動(dòng)跳轉(zhuǎn)到用戶登錄頁面, 輸入賬號和密碼出現(xiàn)賬號密碼校驗(yàn)頁面

以上就是SpringBoot 整合Security權(quán)限控制的初步配置的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Security配置的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決

    idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決

    這篇文章主要介紹了idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 一文學(xué)會如何在SpringBoot中使用線程池執(zhí)行定時(shí)任務(wù)

    一文學(xué)會如何在SpringBoot中使用線程池執(zhí)行定時(shí)任務(wù)

    在開發(fā)現(xiàn)代應(yīng)用程序時(shí),定時(shí)任務(wù)是一項(xiàng)常見的需求,SpringBoot提供了一個(gè)強(qiáng)大的定時(shí)任務(wù)框架,可以輕松地執(zhí)行各種定時(shí)任務(wù),結(jié)合線程池的使用,可以更好地管理任務(wù)的執(zhí)行,提高系統(tǒng)的性能和穩(wěn)定性,本文將介紹如何在Spring Boot中使用線程池執(zhí)行定時(shí)任務(wù)
    2023-06-06
  • RabbitMQ交換機(jī)使用場景和消息可靠性總結(jié)分析

    RabbitMQ交換機(jī)使用場景和消息可靠性總結(jié)分析

    這篇文章主要為大家介紹了RabbitMQ交換機(jī)使用場景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Java中的阻塞隊(duì)列BlockingQueue使用詳解

    Java中的阻塞隊(duì)列BlockingQueue使用詳解

    這篇文章主要介紹了Java中的阻塞隊(duì)列BlockingQueue使用詳解,阻塞隊(duì)列是一種線程安全的數(shù)據(jù)結(jié)構(gòu),用于在多線程環(huán)境下進(jìn)行數(shù)據(jù)交換,它提供了一種阻塞的機(jī)制,當(dāng)隊(duì)列為空時(shí),消費(fèi)者線程將被阻塞,直到隊(duì)列中有數(shù)據(jù)可供消費(fèi),需要的朋友可以參考下
    2023-10-10
  • Java單元測試Powermockito和Mockito使用總結(jié)

    Java單元測試Powermockito和Mockito使用總結(jié)

    公司單元測試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下
    2021-09-09
  • 深入淺析java中finally的用法

    深入淺析java中finally的用法

    finally自己由關(guān)鍵字finally和后面的finally塊組成。這篇文章重點(diǎn)給大家介紹java中finally的用法,需要的朋友參考下吧
    2018-06-06
  • Java設(shè)計(jì)模式之開閉原則精解

    Java設(shè)計(jì)模式之開閉原則精解

    設(shè)計(jì)模式(Design?pattern)代表了最佳的實(shí)踐,通常被有經(jīng)驗(yàn)的面向?qū)ο蟮能浖_發(fā)人員所采用。設(shè)計(jì)模式是軟件開發(fā)人員在軟件開發(fā)過程中面臨的一般問題的解決方案。本篇介紹設(shè)計(jì)模式七大原則之一的開閉原則
    2022-02-02
  • Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用

    Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用

    Spring?Boot2.6.0為我們帶來很多好用的新特性/改進(jìn),這篇文章主要給大家介紹了關(guān)于Spring?Boot2.6.0新特性之默認(rèn)禁止循環(huán)引用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • springboot?vue測試前端項(xiàng)目管理列表分頁功能實(shí)現(xiàn)

    springboot?vue測試前端項(xiàng)目管理列表分頁功能實(shí)現(xiàn)

    這篇文章主要為大家介紹了springboot?vue測試前端項(xiàng)目列表分頁功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Spring之AOP兩種代理機(jī)制對比分析(JDK和CGLib動(dòng)態(tài)代理)

    Spring之AOP兩種代理機(jī)制對比分析(JDK和CGLib動(dòng)態(tài)代理)

    這篇文章主要介紹了Spring之AOP兩種代理機(jī)制對比分析(JDK和CGLib動(dòng)態(tài)代理),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評論