2016-09-30 126 views
0

我们遇到了Spring web服务的问题。 我们使用Spring Security来保护我们的管理后台,以便生成api密钥。当我们在本地机器(Windows和MacOS)上部署它时,它可以正常工作并加载页面。如果我们尝试将它部署到使用Debian或Ubuntu的虚拟机上,那么未加密的端点加载正常,但只要我们点击管理后端,服务器就会锁定并不加载页面。我们尝试使用git repo中的gradle任务bootRun进行部署,编译一个war并将其加载到tomcat实例中,然后编译一个jar并运行它,但都没有成功。我们没有在控制台中看到任何例外情况,但它看起来运行良好,但是,在我们点击后端后,没有其他页面加载,甚至是之前运行的页面。部署在Linux服务器上的Spring Security崩溃

这是安全配置

package me.probE466.config; 

import org.springframework.beans.factory.annotation.Autowired; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.*; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.*; 


@EnableWebSecurity 
@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
//  auth 
//    .inMemoryAuthentication() 
//    .withUser("user").password("password").roles("ADMIN"); 
    } 

    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().ignoringAntMatchers("/post"); 
     http.authorizeRequests() 
       .antMatchers("/admin/**") 
       .authenticated() 
       .antMatchers("/**").permitAll().and().httpBasic(); 
    } 
} 

这是控制器

@RequestMapping(value = "/admin", method = RequestMethod.GET) 
    public ModelAndView getTest() { 
     return new ModelAndView("addapi"); 
    } 

    @RequestMapping(value = "/admin", method = RequestMethod.POST) 
    public 
    @ResponseBody 
    String addApiKey(@RequestParam("userName") String userName) { 
     User user = new User(); 
     String key = generateSecureApiKey(32); 
     user.setUserKey(key); 
     user.setUserName(userName); 
     userRepository.save(user); 
     return key; 
    } 

这是我们的build.gradle

buildscript { 
    ext { 
     springBootVersion = '1.4.1.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'push' 
    version = '0.0.1-SNAPSHOT' 
} 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
} 


dependencies { 
    compile("mysql:mysql-connector-java:5.1.34") 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.thymeleaf:thymeleaf-spring4') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    // https://mvnrepository.com/artifact/commons-lang/commons-lang 
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6' 



    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

任何帮助,将不胜感激

回答

0

好,我们想通了...:

在入门春季安全的(应该已经阅读更接近),它说:

package hello; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/home").setViewName("home"); 
     registry.addViewController("/").setViewName("home"); 
     registry.addViewController("/hello").setViewName("hello"); 
     registry.addViewController("/login").setViewName("login"); 
    } 

} 

这是在我们的配置中缺少。仍然不知道它为什么在我们的客户端上工作(它仍然在没有它的情况下工作),但不在Linux上,但是在我们添加它之后,它在那里也工作得很好。供将来参考:每受保护控制器需要在这里注册...至少在我们的服务器上