【问题标题】:Spring boot disable Redis ServerSpring Boot 禁用 Redis 服务器
【发布时间】:2019-02-04 19:53:08
【问题描述】:

我需要在我的 Spring Boot 应用程序中禁用 redis。 我遵循了很多网上的提示,但没有成功。

我的 application.properties,它有这一行:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

spring.data.redis.repositories.enabled=false

当我尝试启动我的应用程序时,我得到:

说明

org.springframework.session.web.socket.config.annotation.AbstractSessionWebSocketMessageBrokerConfigurer 中的字段 sessionRepository 需要一个无法找到的“org.springframework.session.SessionRepository”类型的 bean。

行动:

考虑在你的配置中定义一个“org.springframework.session.SessionRepository”类型的bean。

我正在运行的应用程序是关于 WebSocket 的测试。它工作得很好,但对于商业事务我需要禁用 Redis。 请,任何帮助将不胜感激。

提前致谢!!

这是我的代码: 我的主要课程:

public class WebSocketChatApplication extends SpringBootServletInitializer {

public static void main(String[] args) {         
SpringApplication.run(WebSocketChatApplication.class, args);    
} 

@Override   protected SpringApplicationBuilder 
   configure(SpringApplicationBuilder application) {        return 
  return application.sources(WebSocketChatApplication.class);   } 
}

这是我的 ChatConfig:

@Configuration
@EnableConfigurationProperties(ChatProperties.class)
 public class ChatConfig {

@Autowired
private ChatProperties chatProperties;

@Bean
@Description("Tracks user presence (join / leave) and broacasts it to all connected users")
public PresenceEventListener presenceEventListener(SimpMessagingTemplate messagingTemplate) {
    PresenceEventListener presence = new PresenceEventListener(messagingTemplate, participantRepository());
    presence.setLoginDestination(chatProperties.getDestinations().getLogin());
    presence.setLogoutDestination(chatProperties.getDestinations().getLogout());
    return presence;
}

@Bean
@Description("Keeps connected users")
public ParticipantRepository participantRepository() {
    return new ParticipantRepository();
}

@Bean
@Scope(value = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Description("Keeps track of the level of profanity of a websocket session")
public SessionProfanity sessionProfanity() {
    return new SessionProfanity(chatProperties.getMaxProfanityLevel());
}

@Bean
@Description("Utility class to check the number of profanities and filter them")
public ProfanityChecker profanityFilter() {
    ProfanityChecker checker = new ProfanityChecker();
    checker.setProfanities(chatProperties.getDisallowedWords());
    return checker;
}

/*@Bean(initMethod = "start", destroyMethod = "stop")
@Description("Embedded Redis used by Spring Session")
public RedisServer redisServer(@Value("${redis.embedded.port}") int port)  throws IOException {
    return new RedisServer(port);
}*/

}

这是我的 WebSocketConfig:

@Configuration
@EnableWebSocketMessageBroker


public class WebSocketConfig extends 
AbstractSessionWebSocketMessageBrokerConfigurer<Session> {



@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/queue/", "/topic/", "/exchange/");
    //registry.enableStompBrokerRelay("/queue/", "/topic/", "/exchange/");
    registry.setApplicationDestinationPrefixes("/app");
}

SecurityConfig 类:

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

private static final String SECURE_ADMIN_PASSWORD = "rockandroll";

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .formLogin()
            .loginPage("/index.html")
            .loginProcessingUrl("/login")
            .defaultSuccessUrl("/chat.html")
            .permitAll()
            .and()
        .logout()
            .logoutSuccessUrl("/index.html")
            .permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/js/**", "/lib/**", "/images/**", "/css/**", "/index.html", "/").permitAll()
            .antMatchers("/websocket").hasRole("ADMIN")
            .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ADMIN")
            .anyRequest().authenticated();
    http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());


}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    auth.authenticationProvider(new AuthenticationProvider() {

        @Override
        public boolean supports(Class<?> authentication) {
            return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
        }

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;

            List<GrantedAuthority> authorities = SECURE_ADMIN_PASSWORD.equals(token.getCredentials()) ? 
                                                    AuthorityUtils.createAuthorityList("ROLE_ADMIN") : null;

            return new UsernamePasswordAuthenticationToken(token.getName(), token.getCredentials(), authorities);
        }
    });
}

}

我认为这是最重要的。剩下的就是一个 RestController 和几个 DTO 对象。 就像我已经说过的,它很好用,但我需要禁用 Redis。

【问题讨论】:

  • 感谢分享代码。您使用的是 Maven 还是 Gradle?您能否提供 pom.xml 或等效项,以便更容易理解类路径中正在加载哪些库? Spring Boot 使用约定优于配置,因此它可能是您正在使用的库正在加载的内容

标签: java spring spring-boot redis


【解决方案1】:

您可以尝试从 Spring Boot 应用程序类中禁用 Redis auto-configuration 以查看您是否有任何不同的行为。

@SpringBootApplication(exclude = RedisAutoConfiguration.class)

【讨论】:

  • 不,相同!如果我添加 spring.session.store-type=none,相同。谢谢!
  • 您能否使用相关代码增强您的问题,以便更容易理解问题可能出在哪里?可能有很多东西。该异常似乎指向 Spring Session 中的问题,但不确定
【解决方案2】:

我在内存会话中实现了 JDBC,它运行良好。 有 1 件事我不明白。 关于会议,我什么时候需要,什么时候不需要? 因为 Spring boot,让你有机会选择 Session Type = none。

谢谢!

【讨论】:

  • 请将问题作为新问题或其他地方的评论发布,而不是作为答案。
  • @GuiRitter 不不不
猜你喜欢
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 2018-02-08
  • 2019-09-06
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
相关资源
最近更新 更多