首页 > 解决方案 > Spring Boot 禁用 Redis 服务器

问题描述

我需要在我的 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);   } 
}

这是我的聊天配置:

@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");
}

安全配置类:

@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。

标签: javaspringspring-bootredis

解决方案


您可以尝试从 Spring Boot 应用程序类中禁用Redis 自动配置,以查看您是否有任何不同的行为。

@SpringBootApplication(exclude = RedisAutoConfiguration.class)

推荐阅读