我使用创建了一个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进行重定向。有什么想法吗?
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();
}发布于 2016-03-28 18:02:38
我已经想明白了。
我工作的公司认为在我们的计算机上运行8080进程是合适的。这意味着我不得不使用8080以外的端口。我用了8083。所以我以为requiresChannel会从8083端口切换到8443端口。需要显式地告诉HttpSecurity从不安全端口到安全的映射。默认情况下,它映射80到443和8080到8443。如果您想要的东西不是默认的,您必须告诉HttpSecurity。下面是一个例子。
@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");
}}
https://stackoverflow.com/questions/36235534
复制相似问题