介绍一下springboot的一些自定义配置。自定义配置前,需要加入一些依赖,在学习笔记1中都要介绍
1.使用springboot自定义拦截器。
首先自己一个拦截器:
1public class MyInterceptor implements HandlerInterceptor { 2 3 @Override 4public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 5 System.out.println("interceptor is working now----"); 6 return true; 7 } 8 9 。。。。。省略其它 10}
然后创建一个类继承 WebMvcConfigurerAdapter。
1@Configuration 2public class WebMvcConfigurer extends WebMvcConfigurerAdapter { 3 4 @Override 5 public void addInterceptors(InterceptorRegistry registry) { 6 //添加自定义拦截器,设置路径 7 registry.addInterceptor(new CustomizeInterceptor()).addPathPatterns("/test/**"); 8 super.addInterceptors(registry); 9 } 10}
2.配置redistemplate
在使用redis 保存key会产生 \xac\xed\x00\x05t\x00这样的乱码,这是因为springboot中默认redistemplate使用的序列化key类不是StringRedisSerializer(),需要我们自己配置一下:
1public static RedisTemplate<String, String> redisTemplate; 2 3 @Autowired public RedisConfigurer(RedisTemplate redisTemplate) { 4 redisTemplate.setKeySerializer(new StringRedisSerializer()); 5 redisTemplate.setValueSerializer(new StringRedisSerializer()); 6 this.redisTemplate = redisTemplate; 7 }
或者(@Configuration + @Bean 注解组合 等价于 xml配置 )
1@Configuration 2public class RedisTemplateUtil { 3 4 @Bean 5 public RedisTemplate<String, String> getRedisTemplate( 6 RedisTemplate redisTemplate) { 7 redisTemplate.setKeySerializer(new StringRedisSerializer()); 8 redisTemplate.setValueSerializer(new StringRedisSerializer()); 9 redisTemplate.setConnectionFactory(getFactory()); 10 return redisTemplate; 11 } 12}
我们配置的redistemplate bean会替换springboot默认创建的redistemplate。 这样直接调用redisTemplate,就会看到可以的乱码问题已经解决。但当我们通过redis-cli查看时,会发现中文value值显示也乱码,通过redis-cli --raw 进入cli,然后查看值就可解决显示问题,该方法值适用于linux系统,windows系统还是会乱码。
3.登陆授权配置:
对于有的web项目,需要用户登录才能授权访问,可以通过以下配置实现用户登录授权:
1@EnableWebSecurity public class SecurityConfigurer extends WebSecurityConfigurerAdapter { 2 3 @Override protected void configure(HttpSecurity http) throws Exception { 4 //指定路径需要登录权限,对静态资源放行,无需授权即可访问 5 http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**", 6 //请求路径包含order的,都需要去登陆 7 "/login").permitAll().antMatchers("/order/**").hasRole(("USER")).and(). 8 formLogin();//在该处追加.loginPage("/tologin"); 9 http.csrf().disable(); 10 //单点登录。如果已经登录,其它登录会使当前登录session失效。进一步操作则需要再次登录 11 http.sessionManagement().maximumSessions(1).expiredUrl("/login"); 12 // BCryptPasswordEncoder 加密类 13 //如果登出,使session无效 14 http.logout().invalidateHttpSession(true); 15 } 16 17 @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 18 auth.inMemoryAuthentication()//全局默认用户 19 .withUser("admin").password("admin").roles("USER"); 20 }
以上配置,会跳转到springboot的默认登陆页面,这种默认页面,缺少样式,不美观。如果需要自定义登陆,只需要Controller写一个处理方法(例如:“/tologin”),然后在在上述类上追加formLogin().loginPage("/tologin”)即可。用户访问的时候,会跳转到自己定义的处理方法。
4.获取配置文件属性:
springboot 提供一个工具类可以加载application.yml中的配置文件,如果要读取application.yml 以下参数:
1spring: 2 username: user 3 password: pass 4 role: 5 - admin 6 - guest
首先要自定义个一个类,做如下设置。
1@Component 2@ConfigurationProperties(prefix = "spring") 3public class PropertiesConfigurer { 4 5 6 private String username; 7 8 private String password; 9 10 private List<String> role=new ArrayList<>(); 11 12 public String getUsername() { 13 return username; 14 } 15 16 public String getPassword() { 17 return password; 18 } 19 20 public List<String> getRole() { 21 return role; 22 } 23}
然后在使用的地方依赖注入:
1@Autowired private PropertiesConfigurer propertiesConfigurer; 2 3 public void () { 4 5....省略 6 7 String name= propertiesConfigurer.getUsername(); 8 9 }