SpringBoot?整合Security權(quá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 會(huì)自動(dòng)跳轉(zhuǎn)到用戶登錄頁面, 輸入賬號(hào)和密碼出現(xiàn)賬號(hào)密碼校驗(yàn)頁面
以上就是SpringBoot 整合Security權(quán)限控制的初步配置的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Security配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Spring gateway配置Spring Security實(shí)現(xiàn)統(tǒng)一權(quán)限驗(yàn)證與授權(quán)示例源碼
- 解決Spring Security的權(quán)限配置不生效問題
- 關(guān)于SpringSecurity配置403權(quán)限訪問頁面的完整代碼
- SpringBoot--- SpringSecurity進(jìn)行注銷權(quán)限控制的配置方法
- spring security動(dòng)態(tài)配置url權(quán)限的2種實(shí)現(xiàn)方法
- Spring Security基于HttpRequest配置權(quán)限示例詳解
相關(guān)文章
idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決
這篇文章主要介紹了idea編譯時(shí)不提示任何錯(cuò)誤信息的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
一文學(xué)會(huì)如何在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ī)使用場(chǎng)景和消息可靠性總結(jié)分析
這篇文章主要為大家介紹了RabbitMQ交換機(jī)使用場(chǎng)景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
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單元測(cè)試Powermockito和Mockito使用總結(jié)
公司單元測(cè)試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測(cè)試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下2021-09-09
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測(cè)試前端項(xiàng)目管理列表分頁功能實(shí)現(xiàn)
這篇文章主要為大家介紹了springboot?vue測(cè)試前端項(xiàng)目列表分頁功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Spring之AOP兩種代理機(jī)制對(duì)比分析(JDK和CGLib動(dòng)態(tài)代理)
這篇文章主要介紹了Spring之AOP兩種代理機(jī)制對(duì)比分析(JDK和CGLib動(dòng)態(tài)代理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

