Spring Boot 整合 Security 許可權控制-3:初步配置 Security

語言: CN / TW / HK

在原始碼目錄下新建 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 *

* http://www.apache.org/licenses/LICENSE-2.0 *

* 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
* 描述 : WebSecurityConfig
* 作者 : qianmoQ
* 版本 : 1.0
* 建立時間 : 2018-03-15 下午3:18
* 聯絡作者 : qianmoQ
/ @Configuration // 開啟security訪問授權 @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()
            // 其他路徑需要授權訪問
            .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();
    // 建立模擬使用者
    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 會自動跳轉到使用者登入頁面, 輸入賬號和密碼出現賬號密碼校驗頁面