我需要在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中的field 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。
答案 0 :(得分:0)
您可以尝试从Spring引导应用程序类中禁用Redis auto-configuration,以查看是否有其他行为。
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
答案 1 :(得分:0)
我在内存会话中实现了JDBC,并且运行良好。 有1件事我不明白。 关于会议,何时需要我什么时候? 由于使用了Spring Boot,因此您有机会选择Session Type = none。
谢谢!