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)建時間 : 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") // 登錄成功后的默認路徑 .defaultSuccessUrl("/").permitAll() // 退出登錄后的默認路徑 .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 會自動跳轉(zhuǎn)到用戶登錄頁面, 輸入賬號和密碼出現(xiàn)賬號密碼校驗頁面
以上就是SpringBoot 整合Security權(quán)限控制的初步配置的詳細內(nèi)容,更多關(guān)于SpringBoot整合Security配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文學(xué)會如何在SpringBoot中使用線程池執(zhí)行定時任務(wù)
在開發(fā)現(xiàn)代應(yīng)用程序時,定時任務(wù)是一項常見的需求,SpringBoot提供了一個強大的定時任務(wù)框架,可以輕松地執(zhí)行各種定時任務(wù),結(jié)合線程池的使用,可以更好地管理任務(wù)的執(zhí)行,提高系統(tǒng)的性能和穩(wěn)定性,本文將介紹如何在Spring Boot中使用線程池執(zhí)行定時任務(wù)2023-06-06RabbitMQ交換機使用場景和消息可靠性總結(jié)分析
這篇文章主要為大家介紹了RabbitMQ交換機使用場景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Java單元測試Powermockito和Mockito使用總結(jié)
公司單元測試框架選用了Junit 4.12,Mock框架選用了Mockito和PowerMock,本文主要介紹了Java單元測試Powermockito和Mockito使用總結(jié),感興趣的可以了解一下2021-09-09Spring?Boot2.6.0新特性之默認禁止循環(huán)引用
Spring?Boot2.6.0為我們帶來很多好用的新特性/改進,這篇文章主要給大家介紹了關(guān)于Spring?Boot2.6.0新特性之默認禁止循環(huán)引用的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-02-02springboot?vue測試前端項目管理列表分頁功能實現(xiàn)
這篇文章主要為大家介紹了springboot?vue測試前端項目列表分頁功能實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Spring之AOP兩種代理機制對比分析(JDK和CGLib動態(tài)代理)
這篇文章主要介紹了Spring之AOP兩種代理機制對比分析(JDK和CGLib動態(tài)代理),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05