SpringSecurity基础(三)通过配置类放行静态界面与过滤器链

一.配置

所有配置如第二小节所示

二.static加入登录界面

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        body {
            background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg') no-repeat;
            background-size: 100% 130%;
        }

        #login_box {
            width: 20%;
            height: 400px;
            background-color: #00000060;
            margin: auto;
            margin-top: 10%;
            text-align: center;
            border-radius: 10px;
            padding: 50px 50px;
        }

        h2 {
            color: #ffffff90;
            margin-top: 5%;
        }

        #input-box {
            margin-top: 5%;
        }

        span {
            color: #fff;
        }

        input {
            border: 0;
            width: 60%;
            font-size: 15px;
            color: #fff;
            background: transparent;
            border-bottom: 2px solid #fff;
            padding: 5px 10px;
            outline: none;
            margin-top: 10px;
        }

        button {
            margin-top: 50px;
            width: 60%;
            height: 30px;
            border-radius: 10px;
            border: 0;
            color: #fff;
            text-align: center;
            line-height: 30px;
            font-size: 15px;
            background-image: linear-gradient(to right, #30cfd0, #330867);
        }

        #sign_up {
            margin-top: 45%;
            margin-left: 60%;
        }

        a {
            color: #b94648;
        }
    </style>
</head>

<body>
<form action="/doLogin" method="post">
    <div id="login_box">
        <h2>LOGIN</h2>
        <div id="input_box">
            <input type="text" value="zhangsan" name="username" placeholder="请输入用户名">
        </div>
        <div class="input_box">
            <input type="password" value="123" name="password" placeholder="请输入密码">
        </div>
        <button type="submit">登录</button>
        <br>
    </div>
</form>
</body>
</html>

三.配置SecurityConfig

注意,这里是springBoot 2.7的写法,与之前的是有区别的
SpringBoot2.7之前的写法与对springSecurity的差异-看看江南大佬怎么描述的吧

这个配置类的作用:
内容一:WebSecurityCustomizer 放行配置
给登录页面放行

放行方式一:
被放行的资源,不需要经过 Spring Security 过滤器链(静态资源一般使用这种)。
放行方式二:
2. 经过 Spring Security 过滤器链,但是不拦截(如果是一个接口想要匿名访问,一般使用这种)

内容二:安全过滤器链:SecurityFilterChain
类似Shiro中xml中配置的过滤器的作用

注意:这个配置类很重要要自己能写出来。

package com.huang.springsecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

    /**
     * 给登录页面放行
     * Spring Security 给一个地址放行,有两种方式:
     * 1. 被放行的资源,不需要经过 Spring Security 过滤器链(静态资源一般使用这种)。
     * 2. 经过 Spring Security 过滤器链,但是不拦截(如果是一个接口想要匿名访问,一般使用这种)。
     * <p>
     * 很明显下面这种方形方式是第一种
     *
     * @return
     */
    @Bean
    WebSecurityCustomizer securityCustomizer() {
        return new WebSecurityCustomizer() {
            @Override
            public void customize(WebSecurity web) {
                web.ignoring().antMatchers("/login.html");
            }
        };
    }

    /**
     * 自己手动配置安全过滤器链
     *
     * @return
     */
    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        //开始认证
        http.authorizeRequests()
                //请求路径如果是 /login.html,则这个请求可以匿名通过
                .antMatchers("/login.html").anonymous()
                //所有的请求,类似于 shiro 中的 /**
                .anyRequest()
                //必须要认证之后才能访问,类似于 shiro 中的 authc
                .authenticated()
                .and()
                //开始配置登录表单
                .formLogin()
                //配置登录页面,如果访问了一个需要认证之后才能访问的页面,那么就会自动跳转到这个页面上来
                .loginPage("/login.html")
                //配置处理登录请求的接口,本质上其实就是配置过滤器的拦截规则,将来的登录请求就会在过滤器中被处理
                .loginProcessingUrl("/doLogin")
                //配置登录表单中用户名的 key
                .usernameParameter("username")
                //配置登录表单中用户密码
                .passwordParameter("password")
                //配置登录成功后的跳转地址
                .defaultSuccessUrl("/hello")
                .failureUrl("/login.html")
                .and()
                //关闭 csrf 防御机制,这个 disable 方法本质上就是从 Spring Security 的过滤器链上移除掉 csrf 过滤器
                .csrf().disable();

        return http.build();
    }
}

四.登录成功展示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值