Spring Secuity學習記錄

WebSecurityConfigurerAdapter:自定義Security策略

AuthenticationManagerBuilder:自定義認證策略

@EnableWebSecurity:開啓WebSecuity模式

SpringSecurity的兩個主要目標是“認證”和“授權”(訪問控制)

“認證”(Authentication)

“授權”(Authorization)

package com.example.springsec;


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;

//必須配置 aop攔截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //授權
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").hasAnyRole("user");//對映權限允許登陸

//            .anyRequest()
//            .authenticated()

        http
                .formLogin()
                .usernameParameter("username") // default is username  默認用戶名
                .passwordParameter("password") // default is password  默認密碼
                .loginPage("/authentication/login") // default is /login with an HTTP get  默認登陸url
                .failureUrl("/authentication/login?failed") // default is /login?error   默認錯誤頁面
                .loginProcessingUrl("/authentication/login/process"); // default is /login  默認登陸成功頁面 // with an HTTP  // post
        http
                .logout()
                .logoutUrl("/")  //登出  url地址
                .logoutSuccessUrl("/") //登出成功 url地址
                .and()
                .httpBasic();

        http    .csrf().disable(); //禁用csrf防禦攻擊

        http.rememberMe().rememberMeParameter("123");//記住賬號  name="123"
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章