说明

在博客用户登录后我想跳转到各自用户的博客首页,我们知道这个地址是动态的。

例如: http://localhost:8080/blog/zhangsan,

每个用户地址不一样。这时候我就用到了自定义登录成功处理器,当然还有失败处理器,大家可以研究研究。

实现效果

1.访问受保护资源

访问:localhost:8080/welcome后跳转到登录页,登录成功后跳转到welcome

2.直接登录

跳转到用户自己的博客首页

实现步骤

1.编写自定义登录成功处理器

package com.laoxu.easyblog.config;

import com.laoxu.easyblog.common.SecurityUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Component; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* @Description: 自定义登录成功处理类
* @Author laoxu
* @Date 2019/5/25 23:32
**/
@Component
public class MyAuthenctiationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8"); RequestCache cache = new HttpSessionRequestCache();
SavedRequest savedRequest = cache.getRequest(request, response);
// 如果来源请求为空则跳转到用户博客首页
String url = "";
if((savedRequest==null)){
url = "/blog/"+ SecurityUtil.getLoginUser();
}else{
url = savedRequest.getRedirectUrl();
} System.out.println(url); response.sendRedirect(url);
}
}

2.添加spring security配置

package com.laoxu.easyblog.config;

import com.laoxu.easyblog.common.MyPasswordEncoder;
import com.laoxu.easyblog.entity.User;
import com.laoxu.easyblog.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; import javax.annotation.Resource;
import javax.sql.DataSource; /**
* Spring Security配置
*
* @author laoxu
* @create 2019-5-24
**/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
MyAuthenctiationSuccessHandler myAuthenctiationSuccessHandler; @Resource
private DataSource dataSource; @Resource(name = "myUserService")
private MyUserService<User> userService; @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(new MyPasswordEncoder());
} @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/fonts/**", "/images/**").permitAll()
.antMatchers("/welcome").hasRole("ADMIN")
.and().formLogin().loginPage("/login").permitAll().successHandler(myAuthenctiationSuccessHandler)
.and().logout().permitAll()
.and().headers().frameOptions().disable() ; //开启记住我功能 }

spring boot整合spring security自定义登录跳转地址的更多相关文章

  1. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

  2. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  3. Spring Boot整合Spring Security自定义登录实战

    本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...

  4. Spring Boot 整合 Spring Security,用户登录慢

    场景 Spring Boot + Spring Security搭建一个Web项目. 临时用了inMemoryAuthentication. @EnableWebSecurity public cla ...

  5. Spring Boot整合Spring Security总结

    一.创建Spring Boot项目 引入Thymeleaf和Web模块以及Spring Security模块方便进行测试,先在pom文件中将 spring-boot-starter-security ...

  6. Spring Boot整合Spring Security

    Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...

  7. Spring Boot整合Spring Batch

    引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...

  8. Spring Boot整合Spring Session实战

    传统java web应用session都是由应用服务器(如tomcat)保存在内存中,这对应但节点应用来说没问题:但对于应用集群来说会造成各节点之间的session无法共享,一个节点挂掉后,其他节点接 ...

  9. Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结

    How to configure Spring Security to allow Swagger URL to be accessed without authentication @Configu ...

  10. Spring Boot 整合Spring Data以及rabbitmq,thymeleaf,向qq邮箱发送信息

    首先得将自己的qq开启qq邮箱的POP3/SMTP服务 说明: p,e为路由key. 用户系统完成登录的时候,将{手机号-时间-IP}保存到队列Phone-queue中,msg-sys系统获得消息打印 ...

随机推荐

  1. [转帖]Python模块winRM

    https://www.jianshu.com/p/ac095497bad4 一.介绍 winRM服务是windows server下PowerShell的远程管理服务.Python脚本通过连接win ...

  2. [转帖]gdb调试常见命令详细总结(附示例操作)

    一.简介 通过gdb调试我们可以监控程序执行的每一个细节,包括变量的值.函数的调用过程.内存中数据.线程的调度等,从而发现隐藏的错误或者低效的代码,程序的调试过程主要有:单步执行,跳入函数,跳出函数, ...

  3. SPEC2006的学习与总结

    SPEC2006的学习与总结 摘要 最近特别想进行一些性能验证工作. 所以研究了spec2006 然后想整理一下之前的内容. 想着将内容整理一下. 这次主要是抄别人的. 知识来源: https://b ...

  4. Oracle12c 快速启动命令设置

    Oracle12c 安装完成之后 一般不会自动启动需要进行一下简单的设置才可以. 方法也比较简单. 可以使用 oracle 自带的 dbstart的命令执行服务启动 需要注意的事项是: 第一修改一个参 ...

  5. 说透IO多路复用模型

    作者:京东零售 石朝阳 在说IO多路复用模型之前,我们先来大致了解下Linux文件系统.在Linux系统中,不论是你的鼠标,键盘,还是打印机,甚至于连接到本机的socket client端,都是以文件 ...

  6. 从零开始配置vim(21)——lsp简介与treesitter 配置

    截止到上一篇文章,我们配置了neovim的很多内容了.具备了一些编辑器的常用功能了,而且可以胜任日常的文档编辑工作了.但是想作为一个可靠的代码编辑器还缺少重要的一环,即代码语法部分的支持. 在过去的v ...

  7. 总结一个问题:csdn发布文章页面为空或者创作内容管理为空

    总结一个问题:csdn发布文章页面或者创作内容管理为空 解决方案: 打开chrome浏览器的设置: 点击清除数据: 选择高级里清除数据,一般24小时就可以了,不行就7天

  8. Python自动化办公--Pandas玩转Excel数据分析【三】

    相关文章: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[二] python处理Excel实现自动化办公教学(含实战)[一] ...

  9. C/C++ Qt 基本文件读写方法

    Qt文件操作有两种方式,第一种使用QFile类的IODevice读写功能直接读写,第二种是利用 QFile和QTextStream结合起来,用流的方式进行文件读写. 第一种,利用QFile中的相关函数 ...

  10. 我管你什么okr还是kpi,PPT轻松交给你

    word一键转ppt 通过[文件]→[选项]→[快速访问工具栏],选择[不在功能区中的命令],找到[发送到Microsoft PowerPoint ],单击[添加]后再[确定] 调出功能按钮后,就可以 ...