九、Spring Boot安全

1.Spring Security简介

  Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型。他能够实现强大的web安全控制。对于安全控制,咱们仅需引入spring-boot-starter-security模块,进行少许的配置,便可实现强大的安全管理。html

  WebSecurityConfigurerAdapter:自定义Security策略web

  AuthenticationManagerBuilder:自定义认证策略spring

  @EnableWebSecurity:开启WebSecurity模式安全

  应用程序的两个主要区域是'认证'和'受权'(或者访问控制)。app

  '认证'和'受权'主要区域是Spring Security 的两个目标。框架

  认证(Authentication),是创建一个他声明的主体的过程(一个'主体'通常是指用户,设备或一些能够在你的应用程序中执行动做的其余系统)ide

  '受权'(Authorization)指肯定一个主体是否容许在你的应用程序执行一个动做的过程。为了抵达须要受权的店,主体的身份已经有认证过程创建。spring-boot

2.Spring Security使用

(1).建立工程

 

(2).引入SpringSecurity

<!--security-->web安全

<dependency>post

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-security</artifactId>

</dependency>

(3).导入文件

(4).SpringSecurity配置类

  HttpSecurity配置登录、注销功能

package com.hosystem.security.config;

 

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.builders.WebSecurity;

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;

 

@EnableWebSecurity

public class MySecurityConfig extends WebSecurityConfigurerAdapter{

 

    //定义受权规则

    @Override

    protected void configure(HttpSecurity http) throws Exception {

//        super.configure(http);

 

        //定制请求的受权规则

        http.authorizeRequests().antMatchers("/").permitAll()

                .antMatchers("/level1/**").hasRole("VIP1")

                .antMatchers("/level2/**").hasRole("VIP2")

                .antMatchers("/level3/**").hasRole("VIP3");

 

        //开启自动配置登陆功能

        http.formLogin();

        //1. /login到登陆页

        //2. 重定向到/login?error表示登陆失败

        //3. 更多详细规定

    }

 

    //定义认证规则

    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

//        super.configure(auth);

 

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

                .withUser("tom").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")

                .and()

                .withUser("jack").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")

                .and()

                .withUser("lucy").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");

 

 

    }

}

注:若是出现There is no PasswordEncoder mapped for the id “null”

    或者 Encoded password does not look like bcrypt(Bad credentials)基本都是springsecurity版本的问题。只须要使用passwordEncoder(new BCryptPasswordEncoder())替换原来的便可。

#老版本springsecurity

auth.inMemoryAuthentication().withUser("user").password("123456").roles("VIP1");

 

#新版本springsecurity

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

        .withUser("tom").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2");

(5).Thymeleaf提供的SpringSecurity标签支持

[1].引入thymeleaf-extras-springsecurity5

<!--springsecurity5-->

<dependency>

   <groupId>org.thymeleaf.extras</groupId>

   <artifactId>thymeleaf-extras-springsecurity5</artifactId>

</dependency>

[2].sec:authorize使用

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org"

     xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1 align="center">欢迎光临武林秘籍管理系统</h1>

<div sec:authorize="!isAuthenticated()">

   <h2 align="center">游客您好,若是想查看武林秘籍 <a th:href="@{/login}">请登陆</a></h2>

</div>

<div sec:authorize="isAuthenticated()">

   <h2><span sec:authentication="name"></span>,你好,你的角色有:

      <span sec:authentication="principal.authorities"></span></h2>

   <form th:action="@{/logout}" method="post">

      <input type="submit" value="注销"

相关文章
相关标签/搜索