Adding Spring 4 websockets to an existing spring project

落花浮王杯 提交于 2019-12-08 09:39:03

问题


My intent is to plug in the spring 4 websocket classes for my existing project. Here is the spring websocket project link: https://github.com/rstoyanchev/spring-websocket-test

problem is my project is using xml configuration files while the websocket project is using very odd java file configurations.

My question is is there a way to import these configurations files from the spring project into my already existing (xml based) project?

for example, my xmls are being refereed in the web.xml, while in the spring websocket project there is no web.xml at all..

P.S Not all configuration classes in that project are components, here are 2 classes without @Configuration anotations:

  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { WebSecurityConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class, WebSocketConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

@Override
protected void customizeRegistration(Dynamic registration) {
    registration.setInitParameter("dispatchOptionsRequest", "true");
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
}

}

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable() //TODO Refactor login form
        .authorizeRequests()
            .antMatchers("/assets/**").permitAll()
            .anyRequest().authenticated()
            .and()
        .logout()
            .logoutSuccessUrl("/login.html?logout")
            .logoutUrl("/logout.html")
            .permitAll()
            .and()
        .formLogin()
            .defaultSuccessUrl("/index.html")
            .loginPage("/login.html")
            .failureUrl("/login.html?error")
            .permitAll();
}


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("fabrice").password("fab123").roles("USER").and()
            .withUser("paulson").password("bond").roles("ADMIN","USER");
}

So will they be picked up in my original scan? how should i integrate them then??

Thanks!


回答1:


Spring 4 is now released, so you can simply include it as a dependency in your project: http://search.maven.org/#artifactdetails%7Corg.springframework%7Cspring-framework-bom%7C4.0.0.RELEASE%7Cpom




回答2:


I have written a detailed tutorial/example on how to use Spring 4 WebSockets with Spring Security 3.2 to 'retro-fit' a regular Spring MVC application with WebSockets. Click here



来源:https://stackoverflow.com/questions/20326421/adding-spring-4-websockets-to-an-existing-spring-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!