美文网首页
SpringBoot+SpringSecurity

SpringBoot+SpringSecurity

作者: inverseli | 来源:发表于2018-09-29 17:52 被阅读0次

SpringSecurity - 认证与授权

SpringSecurity是以认证与授权为主要功能的框架,与其类似的框架有apach shiro框架。

SpringBoot+SpringSecurity案例

引入依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
SpringSecurity配置
package com.inverseli.learning;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author liyuhao
 * @date 2018年9月29日下午12:52:08
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    // 认证
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/")
//              .hasRole(role)
                .permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
    // 授权
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user1").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");


    }
}
配置详解
重写configure方法,进行认证功能实现
  • authorizeRequests() 表示哪些请求需要进行认证,哪些请求不需要认证,除了请求 ' / ' 不需认证,其他请求都需要认证。
  • hasRole() 判断用户角色,如果不符合角色权限,则拒绝访问请求
  • .and() 就是连接作用,如果此处不用and,则使用如下方式
        http
            .authorizeRequests()
                .antMatchers("/")
//              .hasRole(role)
                .permitAll()
                .anyRequest().authenticated()
        http
            .formLogin()
                .loginPage("/login")
                .permitAll()
        http
            .logout()
                .permitAll();
  • http.formLogin() 开启默认登录页面,springboot自动生成login页面,把拦截的请求转到login进行用户登录,如果不开启登录,并且请求被拦截,就会报拒绝访问的错误,而不是转到login页面让用户登录。
  • loginPage() 转到自定义登录界面,springboot默认为" /login "
  • http.logout() 开启默认注销功能
  • http.rememberMe() 开启记住我功能,登录之后把cookie储存在本地,下次登录只要cookie还在就能自动登录
configureGlobal - 授权
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // 低版本授权
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
 @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // 高些的版本授权
        auth
            .inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user1").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");
    }

踩坑

  • 高版本的授权方式改变
  • SpringSecurity中有很多默认实现,换成自定义实现时应避免冲突

相关文章

网友评论

      本文标题:SpringBoot+SpringSecurity

      本文链接:https://www.haomeiwen.com/subject/ulsboftx.html