SpringSecurity表单认证-自定义表单登录页面demo

package com.zcw.demospringsecurity.configuration;

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.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName : WebSecurityConfig
 * @Description : SpringSecurity表单认证-自定义表单登录页面demo
 * @Author : Zhaocunwei
 * @Date: 2020-04-10 15:56
 */
@EnableWebSecurity //添加此注解后,自动被spring发现并注册,
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     *<h1>当我们继承WebSecurityConfigurerAdapter类</h1>
     * 会默认开启csrf()方法
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                //指定处理登录请求的路径
                .loginProcessingUrl("/xxxx")
                //指定登录成功时的处理逻辑
                .successHandler(new AuthenticationSuccessHandler() {
                    @Override
                    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                                        HttpServletResponse httpServletResponse,
                                                        Authentication authentication) throws IOException, ServletException {
                        httpServletResponse.setContentType("application/json;charset=UTF-8");
                        PrintWriter out = httpServletResponse.getWriter();
                        out.write("{\"code\":\"0\",\"msg\":\"登录成功\"}");
                    }
                })
                //登录失败
                .failureHandler(new AuthenticationFailureHandler() {
                    @Override
                    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                                        HttpServletResponse httpServletResponse,
                                                        AuthenticationException e) throws IOException, ServletException {
                        httpServletResponse.setContentType("application/json;charset=UTF-8");
                        httpServletResponse.setStatus(401);
                        PrintWriter out = httpServletResponse.getWriter();
                        out.write("{\"code\":\"401\",\"name\":\""+
                    e.getClass()+"\",\"message\":\""+e.getMessage()+"\"}");

                    }
                })
                .permitAll()//使登录页不设限访问
                .and()//结束当前标签,
                .csrf()
                .disable();
    }

}

发布了458 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_32370913/article/details/105550763