本文参考或摘录自:http://haohaoxuexi.iteye.com/blog/2154714

  在上一篇中使用Spring Security做了一些安全控制,如Spring Security 自动生成登陆页面登陆以后便能正常使用系统。本文介绍Spring Security 自定义登陆页面以及相关的一些处理。

 Spring Security 之form-login。

 1、使用form-login 自定义登陆页

2、使用form-login做登陆引导处理,即登陆成功后定向到其他页面

3、使用form-login 登陆失败后处理

一、form-login 介绍:

form-login元素是用来定义表单登录信息,以及登陆后续处理。配置如下:

<security:http>
<!--http元素下的form-login元素用来定义表单登录信息.login-page指定用户指定的登录页-->
<!-- username-parameter:表示登录时用户名使用的是哪个参数,默认是“j_username”。
password-parameter:表示登录时密码使用的是哪个参数,默认是“j_password”。
login-processing-url:表示登录时提交的地址,默认是“/j-spring-security-check”。这个只是Spring Security用来标记登录页面使用的提交地址,真正关于登录这个请求是不需要用户自己处理的。-->
<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
username-parameter="username"
password-parameter="password"
authentication-success-handler-ref="authSuccess" --用作登陆成功后的处理bean
 /> <!-- IS_AUTHENTICATED_ANONYMOUSLY 表示匿名用户可以访问,与ROLE_ANONYMOUS效果相同 --> <security:intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <security:intercept-url pattern="/**" access="ROLE_USER"/> </security:http>

注:<security:http security="none" pattern="/login.jsp"></security:http> 为不进行安全认证的页面。因此以上配置与以下等效:

<security:http security="none" pattern="/login.jsp"></security:http>
<!--http元素用于定义Web相关权限控制。-->
<!--intercept-url定义了一个权限控制的规则。
pattern属性表示我们将对哪些url进行权限控制,其也可以是一个正则表达式,如上的写法表示我们将对所有的URL进行权限控制;
access属性表示在请求对应的URL时需要什么权限,默认配置时它应该是一个以逗号分隔的角色列表,请求的用户只需拥有其中的一个角色就能成功访问对应的URL。
这里的“ROLE_USER”表示请求的用户应当具有ROLE_USER角色。“ROLE_”前缀是一个提示Spring使用基于角色的检查的标记。-->
<!--注:auto-config="true"时,SpringSecurity发现没有登录回自动创建登陆页面-->
<security:http>
<!--http元素下的form-login元素用来定义表单登录信息.login-page指定用户指定的登录页-->
<!-- username-parameter:表示登录时用户名使用的是哪个参数,默认是“j_username”。
password-parameter:表示登录时密码使用的是哪个参数,默认是“j_password”。
login-processing-url:表示登录时提交的地址,默认是“/j-spring-security-check”。这个只是Spring Security用来标记登录页面使用的提交地址,真正关于登录这个请求是不需要用户自己处理的。-->
<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
username-parameter="username"
password-parameter="password"
authentication-success-handler-ref="authSuccess" --用作登陆成功后的处理bean
/>
<!-- IS_AUTHENTICATED_ANONYMOUSLY 表示匿名用户可以访问,与ROLE_ANONYMOUS效果相同 -->
<!--<security:intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>-->
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>

二、form-login做登陆引导处理 元素:authentication-success-handler-ref。具体配置见上配置,另外如还需使用Spring Security做登陆成功后的引导处理,还需配置如下bean.

 <bean name="authSuccess" class="com.vrv.springMvcDemo.security.AuthenticationSuccessHandlerImpl"></bean>
authSuccess 的实现类代码如下:
package com.vrv.springMvcDemo.security;

import org.springframework.security.core.Authentication;
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; /*
*
* Created by Administrator on 2014/12/5.
*/ public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.sendRedirect("index.jsp");
}
}

如果不需要Spring Security进行登陆成功引导处理,直接使用指定页面则可使用default-target-url 进行配置。如下:

<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
username-parameter="username"
password-parameter="password"
default-target-url="/index.jsp"
/>

三、form-login 登陆失败后处理。配置如下:

<security:http security="none" pattern="/loginFailure.jsp"></security:http>
<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
username-parameter="username"
password-parameter="password"
default-target-url="/index.jsp"
authentication-failure-url="/loginFailure.jsp"
/>

注意:此处loginFailure.jsp需配置为不需要进行安全认证,否则此页面由于Spring Security 而不能访问

同 AuthenticationSuccessHandler 进行登陆成功处理,登陆失败除了可以指定authentication-failure-url之外,form-login同样允许我们指定认证失败后的页面和对应认证失败后的处理器AuthenticationFailureHandler。

Spring Security登陆的更多相关文章

  1. Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”

    编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...

  2. Spring Security 指定登陆入口

    spring security除通过form-login的熟悉指定登陆还可以通过entry-point-ref 指定登陆入口.具体配置如下: <?xml version="1.0&qu ...

  3. spring security+freemarker获取登陆用户的信息

    spring security+freemarker获取登陆用户的信息 目标页面之间获取 ${Session.SPRING_SECURITY_CONTEXT.authentication.princi ...

  4. Ajax登陆,使用Spring Security缓存跳转到登陆前的链接

    Spring Security缓存的应用之登陆后跳转到登录前源地址 什么意思? 用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源. 由 ...

  5. bug日志-天坑,Spring Security的登陆报错:An internal error occurred while trying to authenticate the user.

    在学习Spring Security的时候,我的编辑器给我报错:An internal error occurred while trying to authenticate the user. 明明 ...

  6. 登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

    登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

  7. Spring Security从后台数据库查询实现登陆控制

    Spring Security框架是一个控制登陆的框架,通过配置文件获取后台的用户名及密码,进行比较进行登陆判断 使用步骤 1.导入依赖 <!-- 身份验证 --> <depende ...

  8. Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析

    Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  9. (一)Spring Security Demo 登陆与退出

    文章目录 配置springSecurityFilterChain过滤器 配置身份验证 加载配置 登陆项目 退出 下面的代码需要spring环境的支持: 看这个系列博客之前,需要这个博客,大概了解下 s ...

随机推荐

  1. Java开发人员必须掌握的Linux命令(一)

    子曰:"工欲善其事,必先利其器." 1.登录服务器SSH命令 简单说,SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机, ...

  2. Oracle - Dbms Output window

    Ensure that you have your Dbms Output window open through the view option in the menubar. Click on t ...

  3. Content-type详解

    HttpHeader里的Content-Type 之前一直分不清楚post请求里Content-Type方式,如application/x-www-form-urlencoded.multipart/ ...

  4. Reactor 3 学习笔记(1)

    Reactor 3 与之前学习的RxJava是同一类(反应式编程)框架,基本概念大致差不多,简单记录一下: Reactor 3 利用了java 8中的CompletableFuture.Stream. ...

  5. No module named 'pandas._libs.tslib'

    用pip命令安装: pip install pandas python3的: pip3 install pandas

  6. I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

    问题: 安装TensorFlow(CPU版本),使用pip install tensorflow安装,安装一切顺利,但是在跑一个简单的程序时,遇到如下情况: 大概意思是:你的CPU支持AVX扩展,但是 ...

  7. java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core

    maven 引用 <dependency> <groupId>com.github.dblock</groupId> <artifactId>oshi- ...

  8. zeromq学习笔记1——centos下安装 zeromq-4.1.2

    1.前言 MQ(message queue)是消息队列的简称,可在多个线程.内核和主机盒之间弹性伸缩.ZMQ的明确目标是“成为标准网络协议栈的一部分,之后进入Linux内核”.现在还未看到它们的成功. ...

  9. Web 前端面试题整理(不定时更新)

    重要知识需要系统学习.透彻学习,形成自己的知识链.万不可投机取巧,临时抱佛脚只求面试侥幸混过关是错误的! 面试有几点需注意: 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增 ...

  10. SoapUI Pro Project Solution Collection-XML assert

    in soapui the XML object used here is from  org.w3c.dom package so you need to read this article car ...