Spring Security(Spring安全框架)学习笔记(二)——登录接口,登录参数,登录回调,注销登录

Spring Security(Spring安全框架)学习笔记(一)简介、自定义登录页面、放过静态资源
Spring Security(Spring安全框架)学习笔记(二)登录接口,登录参数,登录回调,注销登录
Spring Security(Spring安全框架)学习笔记(三)返回json格式数据,适用前后端分离场景
Spring Security(Spring安全框架)学习笔记(四)授权操作、权限继承
Spring Security(Spring安全框架)学习笔记(五)整合Mysql数据库

一、配置类 SecurityConfig.java

注意:在写路径的时候要加上"/",否则访问会出错404!

package com.hx.security;

import org.springframework.context.annotation.Bean;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
        // 密码加密实例
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance(); // 采用不加密方式
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置用户名,密码
        //这里的配置会覆盖properties配置文件中配置的账号密码
        // 配置多个使用and连接,一个就不用加and()
        auth.inMemoryAuthentication()
                .withUser("huathy")
                .password("a")
                .roles("admin")
                .and()
                .withUser("worthy")
                .password("a")
                .roles("user");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "images/**");        //放过静态资源下的js,css,img资源
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {    //http安全配置
        //authorizeRequests开启配置
        http.authorizeRequests()
                .anyRequest()           //anyRequest所有请求都拦截
                .authenticated()
                .and()
                .formLogin()                        //formLogin表单配置
                .loginPage("/login.html")           //loginPage指定登录页面(登录接口)
                .loginProcessingUrl("/doLogin")     //指定登录请求接口,若不配置则与指定的loginPage相同
                .usernameParameter("uname")         //指定请求用户名的name属性
                .passwordParameter("pwd")           //指定请求密码的name属性
//                .successForwardUrl("/success")    //成功回调,服务端跳转(地址栏不跳转)转发
                .defaultSuccessUrl("/success",false)     //默认成功跳转页面(地址栏跳转)重定向,
                //会记录重定向地址,即从哪个页面跳转来的,登录成功时就返回哪个页面。参数alwaysUse为true时,与successForwardUrl无异。
//                .failureForwardUrl("/fail")      //服务端跳转,转发
//                .failureUrl("/fail")     //客户端跳转,重定向
                .permitAll()                        //permitAll放过相关页面
                .and()
                .logout()
//                .logoutUrl("/logout")       //配置退出登录地址
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout","POST")) //配置post请求方式的退出登录方法
                .logoutSuccessUrl("/login.html")    //配置退出登录成功的跳转页面
                .invalidateHttpSession(true)        //清除session信息,不配置默认true
                .clearAuthentication(true)          //清除认证信息,不配置默认true
                .and()
                .csrf().disable();                  //关闭csrf
    }

}

二、控制层 HelloController.java

package com.hx.security;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "<h1>HELLO</h1>";
    }

    @RequestMapping("/success")
    public String success(){
        return "<h1>SUCCESS</h1>";
    }
    
    @RequestMapping("/fail")
    public String fail() {
        return "<h1>LOGIN FAIL</h1>";
    }
}

三、html页面 login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/doLogin" method="post">	<!-- 必须指定为post请求,地址为login.html -->
		用户名:<input name="uname"> <br>	<!-- 指定名称username,遵循规范 -->
		密码:<input name="pwd"> <br>	<!-- 指定名称password,遵循规范 -->
		<button type="submit">提交</button>
	</form>
</body>
</html>

四、退出登录logout测试

由于已经将logout更改为post请求方式,故需要发送post请求才可退出登录!
在这里插入图片描述

五、目录结构

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huathy-雨落江南,浮生若梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值