系列博文

  项目已上传至guthub  传送门

  JavaWeb-SpringSecurity初认识  传送门

  JavaWeb-SpringSecurity在数据库中查询登陆用户  传送门

  JavaWeb-SpringSecurity自定义登陆页面  传送门

  JavaWeb-SpringSecurity实现需求-判断请求是否以html结尾  传送门

  JavaWeb-SpringSecurity自定义登陆配置  传送门

  JavaWeb-SpringSecurity图片验证ImageCode  传送门

  JavaWeb-SpringSecurity记住我功能  传送门

  JavaWeb-SpringSecurity使用短信验证码登陆  传送门

  需求

    请求来了,判断请求是否以html结尾,是以html结尾则重定向到登陆页面,不是以html结尾就需要进行身份认证

  首先我们在SecurityConfig.java中configure()方法中修改自定义登陆页面访问路径为/require,打开SpringSecurity对/require请求的身份认证

protected void configure(HttpSecurity http) throws Exception{
//表单验证(身份认证)
http.formLogin()
//自定义登陆页面
.loginPage("/require")
//如果URL为loginPage,则用SpringSecurity中自带的过滤器去处理该请求
.loginProcessingUrl("/loginPage")
.and()
//请求授权
.authorizeRequests()
//在访问我们的URL时,我们是不需要省份认证,可以立即访问
.antMatchers("/login.html","/require").permitAll()
//所有请求都被拦截,跳转到(/login请求中)
.anyRequest()
//都需要我们身份认证
.authenticated()
//SpringSecurity保护机制
.and().csrf().disable();
}

  在controller层下创建SecurityController.java作为用户发起的请求

    @RequestMapping("/require")
public String require()
{
//判断之前的请求是否以html结尾 //如果是,重定向到登陆页面 //如果不是,我们就让他身份认证 return null;
}
package com.Gary.GaryRESTful.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; //Web应用安全适配器
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{ //告诉SpringSecurity密码用什么加密的
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
} protected void configure(HttpSecurity http) throws Exception{
//表单验证(身份认证)
http.formLogin()
//自定义登陆页面
.loginPage("/require")
//如果URL为loginPage,则用SpringSecurity中自带的过滤器去处理该请求
.loginProcessingUrl("/loginPage")
.and()
//请求授权
.authorizeRequests()
//在访问我们的URL时,我们是不需要省份认证,可以立即访问
.antMatchers("/login.html","/require").permitAll()
//所有请求都被拦截,跳转到(/login请求中)
.anyRequest()
//都需要我们身份认证
.authenticated()
//SpringSecurity保护机制
.and().csrf().disable();
} }

SecurityConfig.java

package com.Gary.GaryRESTful.controller;

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

public class SecurityController {

    @RequestMapping("require")
public String require()
{
//判断之前的请求是否以html结尾 //如果是,重定向到登陆页面 //如果不是,我们就让他身份认证 return null;
} }

SecurityController.java

  完成需求编码阶段SecurityController.java

  //拿到转发跳转到之前的请求
private RequestCache requestCache = new HttpSessionRequestCache(); private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); @RequestMapping("/require")
//返回的状态码(401)
@ResponseStatus(code=HttpStatus.UNAUTHORIZED)
public String require(HttpServletRequest request , HttpServletResponse response) throws IOException
{
//拿到了之前的请求
SavedRequest savedRequest = requestCache.getRequest(request, response);
if(savedRequest != null)
{
//url就是引发跳转之前我们的请求
String url = savedRequest.getRedirectUrl();
//判断之前的请求是否以html结尾
if(StringUtils.endsWithIgnoreCase(url, ".html"))
{
//如果是,重定向到登陆页面
redirectStrategy.sendRedirect(request, response, "/login.html");
} } //如果不是,我们就让他身份认证
return new String("需要身份认证");
}
package com.Gary.GaryRESTful.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; @RestController
public class SecurityController { //拿到转发跳转到之前的请求
private RequestCache requestCache = new HttpSessionRequestCache(); //可以用来做重定向
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); @RequestMapping("/require")
//返回的状态码(401)
@ResponseStatus(code=HttpStatus.UNAUTHORIZED)
public String require(HttpServletRequest request , HttpServletResponse response) throws IOException
{
//拿到了之前的请求
SavedRequest savedRequest = requestCache.getRequest(request, response);
if(savedRequest != null)
{
//url就是引发跳转之前我们的请求
String url = savedRequest.getRedirectUrl();
//判断之前的请求是否以html结尾
if(StringUtils.endsWithIgnoreCase(url, ".html"))
{
//如果是,重定向到登陆页面
redirectStrategy.sendRedirect(request, response, "/login.html"); } } //如果不是,我们就让他身份认证
return new String("需要身份认证");
} }

SecurityController.java

  测试阶段

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <h1>Gary登陆页面</h1>
<form action="/loginPage" method="post"> 用户名:
<input type="text" name="username">
<br>
密码:
<input type="password" name="password">
<br>
<input type="submit"> </form> </body>
</html>

login.html

package com.Gary.GaryRESTful.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; //Web应用安全适配器
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{ //告诉SpringSecurity密码用什么加密的
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
} protected void configure(HttpSecurity http) throws Exception{
//表单验证(身份认证)
http.formLogin()
//自定义登陆页面
.loginPage("/require")
//如果URL为loginPage,则用SpringSecurity中自带的过滤器去处理该请求
.loginProcessingUrl("/loginPage")
.and()
//请求授权
.authorizeRequests()
//在访问我们的URL时,我们是不需要省份认证,可以立即访问
.antMatchers("/login.html","/require").permitAll()
//所有请求都被拦截,跳转到(/login请求中)
.anyRequest()
//都需要我们身份认证
.authenticated()
//SpringSecurity保护机制
.and().csrf().disable();
} }

SecurityConfig.java

package com.Gary.GaryRESTful.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; @RestController
public class SecurityController { //拿到转发跳转到之前的请求
private RequestCache requestCache = new HttpSessionRequestCache(); //可以用来做重定向
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); @RequestMapping("/require")
//返回的状态码(401)
@ResponseStatus(code=HttpStatus.UNAUTHORIZED)
public String require(HttpServletRequest request , HttpServletResponse response) throws IOException
{
//拿到了之前的请求
SavedRequest savedRequest = requestCache.getRequest(request, response);
if(savedRequest != null)
{
//url就是引发跳转之前我们的请求
String url = savedRequest.getRedirectUrl();
//判断之前的请求是否以html结尾
if(StringUtils.endsWithIgnoreCase(url, ".html"))
{
//如果是,重定向到登陆页面
redirectStrategy.sendRedirect(request, response, "/login.html"); } } //如果不是,我们就让他身份认证
return new String("需要身份认证");
} }

SecurityController.java

JavaWeb-SpringSecurity实现需求-判断请求是否以html结尾的更多相关文章

  1. JavaWeb之Servlet:请求 与 响应

    1 引入 浏览器和服务器的种类都有很多,要在它们之间通讯,必定要遵循一定的准则,而http协议就是这样的一个"准则". Http协议:规定了 浏览器 和 服务器 数据传输的一种格式 ...

  2. javaweb利用filter拦截请求

    项目上有个小需求,要限制访问者的IP,屏蔽未授权的登录请求.该场景使用过滤器来做再合适不过了. SecurityFilter.java: package com.lichmama.webdemo.fi ...

  3. Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,java 判断请求是不是ajax请求

    Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,java 判断请求是不是ajax请求   Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 java ...

  4. 判断请求是否为ajax

    判断请求是否为ajax 转载:http://www.cnblogs.com/tony-jingzhou/archive/2012/07/30/2615612.html x-requested-with ...

  5. java判断请求是否ajax异步请求

    java判断请求是否ajax异步请求   解决方法: if (request.getHeader("x-requested-with") != null && re ...

  6. thinkphp 判断请求类型

    判断请求类型 在很多情况下面,我们需要判断当前操作的请求类型是GET .POST .PUT或 DELETE,一方面可以针对请求类型作出不同的逻辑处理,另外一方面有些情况下面需要验证安全性,过滤不安全的 ...

  7. php判断请求类型(ajax|get|post|cli)

    php判断请求类型,可以通过 $_SERVER 相关的参数来实现, 这个很在对某些请求代码复用里面很常用.具体代码如下: /** *@todo: 判断是否为post */ if(!function_e ...

  8. JavaWeb之如何把请求数据转成实体类

    JavaWeb之如何把请求数据转成实体类 自己写个工具类加入下面两个静态方法 自定一个注解类DateTimeFormatting 调用方式User user = util.ObjectFromMap( ...

  9. PHP判断请求是否是ajax请求

    首先看一下框架里面是怎样判断的.ThinkPHP:define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && str ...

随机推荐

  1. Python活力练习Day7

    Day7:写出一个程序,接受一个由字母和数字组成的字符串和一个字符,输出输入字符串中含有该字符的个数,不区分大小写 eg:input : a = '123ASVFBVESS'  b = 's' out ...

  2. c语言测试芯片好坏

    问题描述有n个(2<n<20)芯片,好的或坏的,并且有比坏的芯片更多的已知的好的芯片.每个芯片都可以用来测试其他芯片.当用一个好的芯片测试其他芯片时,它可以正确地给出被测芯片是好是坏.当用 ...

  3. Hadoop入门到实战全套大数据Hadoop学习视频

    资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章或者后台回复[Hadoop]获取,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994

  4. LInux基于nginx与OpenSSL实现https访问

    注意!!首先在nginx安装时添加--with-http_ssl_module模块,否则将会报错,只能从头开始了 自建证书: 通过openssl命令(软件包:openssl :openssl-deve ...

  5. BLE 5协议栈-安全管理层

    文章转载自:http://www.sunyouqun.com/2017/04/ 安全管理(Security Manager)定义了设备间的配对过程. 配对过程包括了配对信息交换.生成密钥和交换密钥三个 ...

  6. 适配器 1、ArrayAdapter 2.SimpleAdapter

    1.ArrayAdapter(数组适配器):用于绑定格式单一的数据.数据源:可以是集合或数组 public class MainActivity extends AppCompatActivity { ...

  7. API开发之接口安全(一)----生成sign

    在对于API的开发中 最让人头疼的 就是接口数据暴露 让一些有心之人 抓包之后恶意请求 那么如何解决这一弊端呢?自然而然的 我们就想到了 加密  那我们又如何加密 如何解密 才能使之有最安全的效率呢? ...

  8. 解决docker容器的窗口大小问题

    解决docker容器的窗口大小问题 最近哥们在是使用docker时,发现有些容器内部窗口大小有问题. 如下午所示,vi窗口只占据左上角一部分.正常情况下vi应该铺满整个窗口才对呀. 所以哥们找到了解决 ...

  9. selenium设置Chrome浏览器不出现通知,设置代理IP

    from selenium import webdriver PROXY = "" chrome_options = webdriver.ChromeOptions() prefs ...

  10. MyBatis 分页插件PageHelper 后台报错

    今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...