spring boot 2.0把spring boot data gemfire移除了
写了一个spring-boot-starter-data-gemfire 1.5.7.RELEASE版本的demo
技术jquery easy-ui +spring security+redis+spring-data-gemfire+spring mvc
配置:
package com.mark.demo.security.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
/*
*hxp(hxpwangyi@126.com)
*2017年9月16日
*
*/
@Configuration
public class CaptchaConfiguration {
@Bean
public DefaultKaptcha producer(){
DefaultKaptcha kaptcha=new DefaultKaptcha();
Properties properties=new Properties();
properties.put("kaptcha.border", "yes");
properties.put("kaptcha.image.width", 150);
properties.put("kaptcha.image.height", 40);
properties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
properties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
properties.put("kaptcha.textproducer.font.size", 30);
properties.put("kaptcha.textproducer.char.space", 10);
properties.put("kaptcha.textproducer.font.color", "red");
properties.put("kaptcha.textproducer.char.string", "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
properties.put("kaptcha.textproducer.char.length", 4);
Config config=new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
package com.mark.demo.security.config;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.pdx.PdxSerializer;
import org.apache.geode.pdx.ReflectionBasedAutoSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.cache.GemfireCacheManager;
import org.springframework.data.gemfire.config.annotation.CacheServerConfigurer;
import com.mark.demo.security.utils.SpringUtils;
/*
*hxp(hxpwangyi@126.com)
*2017年10月8日
*
*/
@Configuration
public class GemfireConfig {
@Bean
public CacheServerConfigurer cacheServerPortConfigurer() {
return (beanName, cacheServerFactoryBean) -> {
cacheServerFactoryBean.setBindAddress("localhost");
cacheServerFactoryBean.setPort(40012);
};
}
@Bean
public GemfireCacheManager cacheManager(GemFireCache gemfireCache) {
GemfireCacheManager cacheManager = new GemfireCacheManager();
cacheManager.setCache(gemfireCache);
return cacheManager;
}
@Bean("gemfireCache")
public CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true);
gemfireCache.setCopyOnRead(true);
return gemfireCache;
}
@Bean
public PdxSerializer compositePdxSerializer() {
return new ReflectionBasedAutoSerializer();
}
/* @Bean
public JtaTransactionManager transactionManager(UserTransaction userTransaction) {
JtaTransactionManager transactionManager = new JtaTransactionManager();
transactionManager.setUserTransaction(userTransaction);
return transactionManager;
}*/
@Bean("userGemfireTemplate")
public GemfireTemplate userGemfireTemplate(){
GemfireTemplate template=new GemfireTemplate();
template.setRegion(SpringUtils.getBean("user"));
return template;
}
@Bean("resourceGemfireTemplate")
public GemfireTemplate resourceGemfireTemplate(){
GemfireTemplate template=new GemfireTemplate();
template.setRegion(SpringUtils.getBean("resource"));
return template;
}
@Bean("menuGemfireTemplate")
public GemfireTemplate menuGemfireTemplate(){
GemfireTemplate template=new GemfireTemplate();
template.setRegion(SpringUtils.getBean("menu"));
return template;
}
}
package com.mark.demo.security.config;
import java.lang.reflect.Method;
import java.time.Duration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/*
*hxp(hxpwangyi@126.com)
*2017年9月16日
*
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public KeyGenerator wiselyKeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
/*@Bean
public CacheManager cacheManager(
@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
*/
@Bean("myRedisTemplate")
public RedisTemplate<String,Object> redisTemplate(
RedisConnectionFactory factory) {
RedisTemplate<String,Object> template = new RedisTemplate<String,Object>();
template.setConnectionFactory(factory);
/*Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om); */
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
package com.mark.demo.security.config;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import com.mark.demo.security.session.RedisSessionManager;
import com.mark.demo.security.utils.JedisUtils;
import com.mark.demo.security.utils.SpringUtils;
/*
*hxp(hxpwangyi@126.com)
*2017年9月16日
*
*/
@Configuration
public class UtilConfig {
@Bean(name="springUtils")
public SpringUtils springUtils(ApplicationContext applicationContext){
SpringUtils springUtils=new SpringUtils();
springUtils.setApplicationContext(applicationContext);
return springUtils;
}
@Bean
public RedisSessionManager redisSessionManager(){
RedisSessionManager manager=new RedisSessionManager();
return manager;
}
@Bean
public JedisUtils jedisUtil(RedisTemplate<String, Object> redisTemplate){
JedisUtils jedisUtil=new JedisUtils();
jedisUtil.setRedisTemplate(redisTemplate);
return jedisUtil;
}
}
package com.mark.demo.security.config;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import com.mark.demo.security.repository.UserRepository;
import com.mark.demo.security.security.CustomAccessDecisionManager;
import com.mark.demo.security.security.CustomFilterSecurityInterceptor;
import com.mark.demo.security.security.CustomLogoutSuccessHandler;
import com.mark.demo.security.security.CustomSavedRequestAwareAuthenticationSuccessHandler;
import com.mark.demo.security.security.CustomUserDetailsService;
import com.mark.demo.security.security.CustomUsernamePasswordAuthenticationFilter;
import com.mark.demo.security.session.RedisSessionManager;
/*
*hxp(hxpwangyi@126.com)
*2017年9月24日
*
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserRepository userRepository;
@Autowired
private RedisSessionManager redisSessionManager;
@Autowired
private CustomFilterSecurityInterceptor customFilterSecurityInterceptor;
@Autowired
private CustomLogoutSuccessHandler customLogoutSuccessHandler;
@Autowired
private CustomSavedRequestAwareAuthenticationSuccessHandler customSavedRequestAwareAuthenticationSuccessHandler;
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Autowired
private CustomAccessDecisionManager customAccessDecisionManager;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterBefore(customFilterSecurityInterceptor, FilterSecurityInterceptor.class)
.addFilterAt(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests().accessDecisionManager(customAccessDecisionManager)
.antMatchers("/js/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/common/login**").permitAll()
.antMatchers("/captcha**").permitAll()
.anyRequest().authenticated()
.antMatchers("/admins/**").hasAuthority("ROLE_ADMIN")
.and()
.formLogin()
.loginPage("/common/login")
.usernameParameter("userName").passwordParameter("password")
.loginProcessingUrl("/common/login/submitlogin")
.successForwardUrl("/admins/indexes/index")
.failureForwardUrl("/common/login?error=true")
.permitAll()
.successHandler(customSavedRequestAwareAuthenticationSuccessHandler)
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/logout")
.logoutSuccessHandler(customLogoutSuccessHandler)
.permitAll()
.invalidateHttpSession(true)
.and()
.rememberMe()
.tokenValiditySeconds(1209600)
.and().sessionManagement()
.invalidSessionUrl("/common/login")
.maximumSessions(1)
.expiredUrl("/common/login?expire=true");
}
@Bean
public CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter()throws Exception{
CustomUsernamePasswordAuthenticationFilter filter=new CustomUsernamePasswordAuthenticationFilter();
filter.setUserRepository(userRepository);
filter.setRedisSessionManager(redisSessionManager);
filter.setAuthenticationManager(authenticationManager());
filter.setFilterProcessesUrl("/common/login/submitlogin");
filter.setAuthenticationSuccessHandler(customSavedRequestAwareAuthenticationSuccessHandler);
filter.setAuthenticationFailureHandler(simpleUrlAuthenticationFailureHandler());
return filter;
}
@Bean
public SimpleUrlAuthenticationFailureHandler simpleUrlAuthenticationFailureHandler(){
SimpleUrlAuthenticationFailureHandler handler=new SimpleUrlAuthenticationFailureHandler();
handler.setDefaultFailureUrl("/common/login?error=code");
return handler;
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(){
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(customUserDetailsService);
return daoAuthenticationProvider;
}
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
ProviderManager authenticationManager = new ProviderManager(Arrays.asList(daoAuthenticationProvider()));
authenticationManager.setEraseCredentialsAfterAuthentication(false);
return authenticationManager;
}
}
启动类:
package com.mark.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
import org.springframework.data.gemfire.config.annotation.EnablePdx;
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
/*
*hxp(hxpwangyi@126.com)
*2017年9月16日
*
*/
@EnableAutoConfiguration
@SpringBootApplication
@PeerCacheApplication
@CacheServerApplication(locators = "localhost[1099]",logLevel = "info", autoStartup = true, maxConnections = 100)
@EnablePdx(serializerBeanName = "compositePdxSerializer")
@EnableEntityDefinedRegions(basePackages = {"com.mark.demo.security.entity"})
@EnableGemfireRepositories(basePackages={"com.mark.demo.security.repository"})
@ComponentScan
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties:
server.context-path=/aaa
cookie.domain=localhost
cookie.sessionId=JSESSIONID
cookie.rememberName=MARK_REMEMBER_ID
cookie.path=/
cookie.maxage=2592000
user.multiAccountLogin=true
# REDIS (RedisProperties)
#spring.redis.host=${p.redis.master.host}
#spring.redis.port=${p.redis.master.port}
#spring.redis.pool.max-idle=300
#spring.redis.pool.min-idle=0
#spring.redis.pool.max-active=600
#spring.redis.pool.max-wait=-1
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html; charset=UTF-8
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
#spring.freemarker.prefix=
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=/WEB-INF/ftl/
#spring.mvc.view.prefix=/WEB-INF/view/
#spring.mvc.view.suffix=.jsp
完整代码地址: 完整代码