springsecurity表单认证

1.登录

  • 创建 SecurityConfig 配置类 继承 SecurityConfig 重写 configure方法
  • http.formLogin() 启用表单登录
  • http.loginPage("/authentication/require") 当请求需要身份认证时,默认跳转的url,就是登录页面
  • http.loginProcessingUrl("/authentication/form")默认的用户名密码登录请求处理url,form表单action的url
  • 登录时候需要判断 是html请求还是 app这样的请求 ,登录时 springSecurity 判断需要身份认证时候将请求信 缓存到 RequestCache 里面,当跳转到 登录页面controller时候从 RequestCache 里面取出 请求类型 在判断返回页面还是 json
private RequestCache requestCache = new RequestCache ();
  • 将html页面 地址配置在 yml里面 用实体类 映射yml,实体类中的url设置默认值,如果配置文件没有设置去默认值

2.登录成功处理

  • 创建类继承 extends SavedRequestAwareAuthenticationSuccessHandler ,重写onAuthenticationSuccess 方法 处理登录成功后的 逻辑
@Component("imoocAuthenticationSuccessHandler")
public class ImoocAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SecurityProperties securityProperties;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {
        logger.info("登录成功");
        if (LoginResponseType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(authentication));
        } else {
            super.onAuthenticationSuccess(request, response, authentication);
        }
    }
}
  • http.successHandler() 配置登录成功后的controller
public class AbstractChannelSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    protected AuthenticationSuccessHandler imoocAuthenticationSuccessHandler;

    @Autowired
    protected AuthenticationFailureHandler imoocAuthenticationFailureHandler;

    protected void applyPasswordAuthenticationConfig(HttpSecurity http) throws Exception {
        http.formLogin()
            .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
            .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)
            .successHandler(imoocAuthenticationSuccessHandler)
            .failureHandler(imoocAuthenticationFailureHandler);
    }

}

3 登录失败处理

  • 登录失败与登录成功处理类似
  • failureHandler(imoocAuthenticationFailureHandler); 登录失败处理controller
@Component("imoocAuthenctiationFailureHandler")
public class ImoocAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException {
        logger.info("登录失败");
        if (LoginResponseType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(new SimpleResponse(exception.getMessage())));
        }else{
            super.onAuthenticationFailure(request, response, exception);
        }
    }
}

4 用户信息共享

  • securityContext 过滤器检查 session是否有 用户信息 有 放到线程,当结束时 securityContext滤器 检查线程是否有 contgext 有放到session

原文地址:https://www.cnblogs.com/tekken-wang/p/12162649.html

时间: 2025-02-01 12:48:14

springsecurity表单认证的相关文章

SharePoint 2013 表单认证使用ASP.Net配置工具添加用户

前 言 上面一篇博客,我们了解到如何为SharePoint 2013配置表单身份认证,但是添加用户是一个麻烦事儿:其实,我们还可以用Asp.Net的配置工具,为SharePoint 2013添加表单用户,下面让我们简单介绍下,如何操作. 打开Visual Studio,新建项目,选择Asp.net web application类型,如下图: 点击OK,进入选择模板页面,如下图: 创建完毕,双击打开web.config,如下图: 添加数据库连接串,连接的是我们表单认证的数据库,不要写错了,如下图

SharePoint 2013 表单认证使用ASP.Net配置工具加入用户

前 言 上面一篇博客,我们了解到怎样为SharePoint 2013配置表单身份认证.可是加入用户是一个麻烦事儿:事实上,我们还能够用Asp.Net的配置工具,为SharePoint 2013加入表单用户,以下让我们简介下.怎样操作. 打开Visual Studio.新建项目,选择Asp.net web application类型.例如以下图: 点击OK,进入选择模板页面.例如以下图: 创建完成,双击打开web.config,例如以下图: 加入数据库连接串,连接的是我们表单认证的数据库,不要写错

SharePoint 2013 基于数据库的表单认证FBA,并添加注册界面

分三块: 1.配置  2.数据库添加用户 3.创建注册的页面 大纲  见黄色高亮处 1 先参考:   把配置文件弄好 http://www.cnblogs.com/jianyus/p/4617548.html SharePoint 2013 配置基于表单的身份认证 前 言 这里简单介绍一下为SharePoint 2013 配置基于表单的身份认证,简单的说,就是用Net提供的工具创建数据库,然后配置SharePoint 管理中心.STS服务.Web应用程序的三处web.config即可.下面,让我

Form authentication(表单认证)问题

前言 最近在做ASP.NET MVC中表单认证时出了一些问题,特此记录. 问题 进行表单认证时,在 PostAuthenticateRequest 事件中从Cookie值中解密票据.如下: protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if

Spring Security应用开发(05)自定义表单认证

Spring Security自动产生的登录页面非常简陋,但是Spring Security提供了丰富的自定义功能. 1.1.1. 密码摘要处理 数据库中的密码字段可以使用SHA摘要算法处理后再保存,而不是以明文保存. mysql> select * from users; +----------+----------+---------+ | username | password | enabled | +----------+----------+---------+ | lisi | 1

Django 中的Form表单认证

一.Form表单 ? 1.1 Form的几个功能 验证用户数据(显示错误信息) 初始化页面显示内容 HTML Form提交保留上次提交数据 生成HTML标签 ? 1.2 创建表单类Form 1. 创建的类必须继承类:forms.Form 2. 字段名必须与前端表单中的名称一致(完全一样) 3. Field类是所有其他类的基类 #!/usr/bin/env python # -*- coding:utf-8 -*- import re from django import forms from d

01.Spring Security初识,表单认证

初识spring security <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.securi

Django之form表单认证

Model常用操作: - 参数:filter 三种传参方式 - all(得到的是列表),values(字典),values_list(元祖) [obj(id,name,pwd,email),obj(id,name,pwd,email),] models.UserInfo.objects.all() #取到所有的值 [obj(id,name,email)] # pwd未取值 data_list = models.UserInfo.objects.all().only('name','email')

SpringSecurity 默认表单登录页展示流程源码

SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityInterceptor, 2.ExceptionTranslationFilter , 3.DefaultLoginPageGeneratingFilter 过滤器, 并且简单介绍了 AccessDecisionManager 投票机制 ?1.准备工作(体验SpringSecurity默认表单认证) ??1.1 创