首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当重定向到https时,我在url中得到一个双根上下文。

问当重定向到https时,我在url中得到一个双根上下文。
EN

Stack Overflow用户
提问于 2016-03-26 12:54:11
回答 1查看 144关注 0票数 0

我使用创建了一个HelloWorld Spring应用程序。它有一个控制器和一个jsp。应用程序显示"Hello!“在网页上。我不会为此发布代码(除非被问到),因为它是琐碎和不重要的。我正在使用关键的tc Server Developer Edition v3.1 (附带Spring工具套件)。

因此,我添加了Spring4安全性,保护根(requiresChannel().antMatchers("/").requiresSecure();)并进行部署。

我在浏览器中输入这个"http://localhost:8081/helloWorld/“,然后重定向到"http://localhost:8081/helloWorld/helloWorld/”。

我已经使用了tc-server/tomcat中的所有默认值。我什么都没变。

我猜Tomcat正在这样做,我必须关闭Tomcat重定向,因为我使用Spring进行重定向。有什么想法吗?

代码语言:javascript
复制
package com.test.security;

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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws     Exception
  {
    System.out.println(System.getProperty("env"));
    if(System.getProperty("env").equals("dit"))
    {
      auth.inMemoryAuthentication()
        .withUser("user").password("user").roles("USER").and()
        .withUser("admin").password("admin").roles("USER", "ADMIN");
    }
    else if(System.getProperty("env").equals("sit"))
    {
      auth.inMemoryAuthentication()
      .withUser("user").password("user").roles("USER").and()
      .withUser("admin").password("admin").roles("USER", "ADMIN");
    }
    else if(System.getProperty("env").equals("uat"))
    {
      auth.inMemoryAuthentication()
      .withUser("user").password("user").roles("USER").and()
      .withUser("admin").password("admin").roles("USER", "ADMIN");
    }
    else if(System.getProperty("env").equals("prd"))
    {
      auth.inMemoryAuthentication()
      .withUser("user").password("user").roles("USER").and()
      .withUser("admin").password("admin").roles("USER", "ADMIN");
    }
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
    http.authorizeRequests()
          .anyRequest().permitAll()
        .and()
        .formLogin()
        .and()
        .httpBasic()
        .and()
        .requiresChannel()
           .antMatchers("/").requiresSecure();
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-28 18:02:38

我已经想明白了。

我工作的公司认为在我们的计算机上运行8080进程是合适的。这意味着我不得不使用8080以外的端口。我用了8083。所以我以为requiresChannel会从8083端口切换到8443端口。需要显式地告诉HttpSecurity从不安全端口到安全的映射。默认情况下,它映射80到443和8080到8443。如果您想要的东西不是默认的,您必须告诉HttpSecurity。下面是一个例子。

代码语言:javascript
复制
@Configuration
@EnableWebSecurity
public class PortMapperSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
       http.authorizeRequests().antMatchers("/**").hasRole("USER").and().formLogin()
            .permitAll().and()
            .portMapper().http(8083).mapsTo(8443).http(80).mapsTo(443);
    }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception
  {
    auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
  }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36235534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档