springboot shiro 解决https redirect重定向后https变成http问题

本文介绍了一种通过Spring Boot配置过滤器实现HTTP到HTTPS重定向的方法。利用AbsoluteSendRedirectFilter过滤器,可以确保所有HTTP请求被重定向到HTTPS,增强网站安全性。

配置过滤器

import com.lenovo.ruler.manage.system.filter.AbsoluteSendRedirectFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "server.scheme.chanage.enabled", havingValue = "true") // 开启注解才会启动
public class RedirectFilterConfig {

    @Bean
    public FilterRegistrationBean registFilter() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AbsoluteSendRedirectFilter());
        registration.addUrlPatterns("*");
        registration.setName("filterRegistrationBean");
        registration.setOrder(1);
        return registration;
    }

}

 

import org.springframework.web.filter.OncePerRequestFilter;

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

public class AbsoluteSendRedirectFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        RedirectResponseWrapper redirectResponseWrapper = new RedirectResponseWrapper(request, response);
        filterChain.doFilter(request, redirectResponseWrapper);
    }

}

http重写成https

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class RedirectResponseWrapper extends HttpServletResponseWrapper {

    private final HttpServletRequest request;

    public RedirectResponseWrapper(final HttpServletRequest inRequest, final HttpServletResponse response) {
        super(response);
        this.request = inRequest;
    }

    @Override
    public void sendRedirect(final String pLocation) throws IOException {

        if (StringUtils.isBlank(pLocation)) {
            super.sendRedirect(pLocation);
            return;
        }

        try {
            final URI uri = new URI(pLocation);
            if (uri.getScheme() != null) {
                super.sendRedirect(pLocation);
                return;
            }
        } catch (URISyntaxException ex) {
            super.sendRedirect(pLocation);
        }

        // !!! FIX Scheme  !!!
        String finalurl = "https://" + this.request.getServerName();
        if (request.getServerPort() != 80 && request.getServerPort() != 443) {
            finalurl += ":" + request.getServerPort();
        }
        finalurl += pLocation;
        super.sendRedirect(finalurl);
    }

}

<think>好的,我现在需要帮助用户了解在Spring Boot Web后端如何实现重定向功能。首先,我得回忆一下Spring Boot中处理重定向的几种常见方法。用户可能已经看过一些文档或者示例,但需要更清晰的指导步骤。 根据用户提供的引用内容,特别是引用[2]提到了返回redirect,这应该是指使用RedirectView或者返回带有"redirect:"前缀的字符串。另外,引用[3]提到了Shiro在未登录时重定向问题,这可能涉及到处理重定向时的协议问题(比如HTTPHTTPS),不过用户当前的问题更基础,是如何实现重定向,而不是处理其引发的问题。 首先,我应该介绍最常见的两种方法:使用RedirectView和返回带有"redirect:"的字符串。这两种方法在Spring MVC中都是标准的,适用于Spring Boot项目。 然后,用户可能需要知道如何传递参数,比如在重定向时带查询参数。这可以通过在重定向路径后添加参数来实现,或者在RedirectAttributes中添加Flash属性。Flash属性适用于需要在重定向后仍然保持的数据,比如在表单提交后显示成功消息。 接下来,用户的问题可能涉及RESTful API中的重定向,这时候返回ResponseEntity会更合适。比如,当创建资源后返回303状态码指向新资源的位置。需要展示如何构建ResponseEntity对象,并设置状态码和Location头。 另外,需要提醒用户关于配置ServletComponentScan,如引用[1]中的代码所示,这可能涉及到Servlet组件的扫描,比如Filter或Servlet的注册,但重定向通常不需要这个,除非用户有自定义的Servlet组件。 还要注意,在前后端分离的项目中,重定向可能需要返回JSON而不是视图,如引用[3]所述的情况,但用户当前的问题是关于后端实现,所以可能不需要深入讨论前后端分离的处理,除非用户有进一步的问题。 最后,需要确保示例代码的正确性,比如控制器类使用@Controller或@RestController注解,方法使用@RequestMapping或其派生注解如@GetMapping、@PostMapping等。同时,要检查重定向路径是否正确,是否需要上下文路径等。 总结回答的结构应该分为几个方法,每种方法给出示例代码,并简要说明适用场景。然后生成相关问题,引导用户进一步学习,比如性能优化、参数传递或异常处理。</think>在Spring Boot Web后端实现重定向功能可以通过以下4种方式实现: ### 一、基础重定向方法 1. **RedirectView方式** ```java @GetMapping("/old") public RedirectView redirectOld() { return new RedirectView("/new"); // 直接返回视图对象 } ``` 2. **字符串前缀方式(最常用)** ```java @GetMapping("/legacy") public String redirectLegacy() { return "redirect:/modern"; // 自动处理上下文路径 } ``` ### 二、参数传递实现 1. **URL参数拼接** ```java @GetMapping("/search") public String redirectWithParams(@RequestParam String keyword) { return "redirect:/result?q=" + URLEncoder.encode(keyword, StandardCharsets.UTF_8); } ``` 2. **使用RedirectAttributes(推荐)** ```java @PostMapping("/submit") public String formSubmit(RedirectAttributes attributes) { attributes.addAttribute("msg", "操作成功"); // URL参数 attributes.addFlashAttribute("alert", "请查收邮件"); // Session临时存储 return "redirect:/status"; } ``` ### 三、HTTP状态码控制 1. **ResponseEntity方式(RESTful API适用)** ```java @GetMapping("/api/resource") public ResponseEntity<Void> redirectApi() { HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create("/api/v2/resource")); return new ResponseEntity<>(headers, HttpStatus.PERMANENT_REDIRECT); } ``` 2. **Servlet原生方式** ```java @GetMapping("/legacy-system") public void servletRedirect(HttpServletResponse response) throws IOException { response.sendRedirect("/modern-system"); } ``` ### 四、配置注意事项 1. 确保应用入口类包含`@SpringBootApplication`注解 ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 2. 重定向路径规则: - 以`/`开头:相对于应用上下文路径(如`http://host/context/new`) - 无`/`开头:相对于当前请求路径 3. 安全建议: ```properties # application.properties server.use-forward-headers=true # 代理环境下保持正确协议 ```
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值