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

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

文章目录

  • 一、配置类 SecurityConfig.java
  • 二、控制层 HelloController.java
  • 三、html页面 login.html
  • 四、退出登录logout测试
  • 五、目录结构

一、配置类 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 "

HELLO

"
; } @RequestMapping("/success") public String success(){ return "

SUCCESS

"
; } @RequestMapping("/fail") public String fail() { return "

LOGIN FAIL

"
; } }

三、html页面 login.html


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
	<form action="/doLogin" method="post">	
		用户名:<input name="uname"> <br>	
		密码:<input name="pwd"> <br>	
		<button type="submit">提交button>
	form>
body>
html>

四、退出登录logout测试

由于已经将logout更改为post请求方式,故需要发送post请求才可退出登录!
Spring Security(Spring安全框架)学习笔记(二)——登录接口,登录参数,登录回调,注销登录_第1张图片

五、目录结构

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

你可能感兴趣的:(#,Java框架学习)