springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
来源:互联网 发布:win8.1优化 编辑:程序博客网 时间:2024/05/17 07:31
介绍一下springboot的一些自定义配置。自定义配置前,需要加入一些依赖,在学习笔记1中都要介绍
使用springboot自定义拦截器
首先自己一个拦截器:
public class MyInterceptor implements HandlerInterceptor {@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("interceptor is working now----");return true;}。。。。。省略其它}
然后创建一个类继承 WebMvcConfigurerAdapter如下即可配置成功:
@Configurationpublic class WebMvcConfigurer extends WebMvcConfigurerAdapter {@Override public void addInterceptors(InterceptorRegistry registry) { //添加自定义拦截器,设置路径registry.addInterceptor(new CustomizeInterceptor()).addPathPatterns("/test/**");super.addInterceptors(registry);}}
配置redistemplate
在使用redis 保存key会产生 \xac\xed\x00\x05t\x00这样的乱码,这是因为springboot中默认redistemplate使用的序列化key类不是StringRedisSerializer(),需要我们自己配置一下:
public static RedisTemplate<String, String> redisTemplate;@Autowired public RedisConfigurer(RedisTemplate redisTemplate) {redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());this.redisTemplate = redisTemplate;}
注意:在使用的过程中,直接注入RedisTemplate 类就可以啦!因为我们配置的redistemplate bean会替换springboot默认创建的redistemplate。 测试一下,就会看到可以的乱码问题已经解决。但当我们通过redis-cli在服务器上查看时,会发现中文value值显示也乱码,通过redis-cli --raw 进入cli,然后查看值就可解决显示问题,该方法值适用于linux系统,windows系统还是会乱码。
登陆授权配置:
对于有的web项目,需要用户登录才能授权访问,可以通过以下配置实现用户登录授权:
@EnableWebSecurity public class SecurityConfigurer extends WebSecurityConfigurerAdapter {@Override protected void configure(HttpSecurity http) throws Exception {//指定路径需要登录权限,对静态资源放行,无需授权即可访问http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**", //请求路径包含order的,都需要去登陆 "/login").permitAll().antMatchers("/order/**").hasRole(("USER")).and(). formLogin();//在该处追加.loginPage("/tologin");http.csrf().disable();//单点登录。如果已经登录,其它登录会使当前登录session失效。进一步操作则需要再次登录http.sessionManagement().maximumSessions(1).expiredUrl("/login");//BCryptPasswordEncoder 加密类//如果登出,使session无效http.logout().invalidateHttpSession(true);}@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication()//全局默认用户.withUser("admin").password("admin").roles("USER");}
以上配置,会跳转到springboot的默认登陆页面,这种默认页面,缺少样式,不美观。如果需要自定义登陆,只需要Controller写一个处理方法(例如:“/tologin”),然后在在上述类上追加formLogin().loginPage("/tologin”)即可。用户访问的时候,会跳转到自己定义的处理方法。
获取配置文件属性
方法一
springboot 提供一个工具类可以加载application.yml中的配置文件,如果要读取application.yml 以下参数:
spring: username: user password: pass role: - admin - guest
首先要自定义个一个类,做如下设置。
@Component@ConfigurationProperties(prefix = "spring")public class PropertiesConfigurer { private String username; private String password; private List<String> role=new ArrayList<>();public String getUsername() {return username;}public String getPassword() {return password;}public List<String> getRole() {return role;}}
然后在使用的地方依赖注入:
@Autowired private PropertiesConfigurer propertiesConfigurer; public void () { ....省略 String name= propertiesConfigurer.getUsername(); }
使用@value注解直接获取:
private @Value("${spring.username.user: }") String user;
注意.这样注解使用,一定要保证配置文件有该key存在,如果该key可有可无,一定要像上述一样,在key后面加: 。
阅读全文
0 0
- springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
- SpringBoot学习记录3------外部配置文件yml
- 读取后缀(yml)配置文件
- 使用SpringBoot yml配置文件(三)
- 使用SpringBoot yml配置文件(三)
- SpringBoot配置文件application.yml (application.properties)
- SpringBoot .yml配置文件
- SpringBoot学习笔记(六):配置拦截器,控制登录跳转
- SpringBoot开发详解(三)--SpringBoot配置文件YML注意事项
- SpringBoot开发详解(三)--SpringBoot配置文件YML注意事项
- SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
- SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
- SpringBoot学习:获取yml和properties配置文件的内容(转载)
- SpringBoot学习:获取yml和properties配置文件的内容
- SpringBoot学习:获取yml和properties配置文件的内容
- springboot注解读取yml文件
- SpringBoot学习笔记之拦截器使用
- springboot学习总结系列(一):多环境下(开发环境,生产环境)使用yml 或 properties 配置文件
- android 单独编译系统apk 和 systemimage
- AJAX参数的使用
- 文章学习《HMM学习最佳范例》
- 9.6联合作战战果
- 分割与增强
- springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
- StringUtils详解
- mysql主从复制(超简单)
- HDU
- 梳理 Opengl ES 3.0 (二)剖析一个GLSL程序
- OpenCV在图像中添加文字,画点,画直线
- JZOJ1294.【USACO题库】3.4.2 American Heritage美国血统
- hdu3974(dfs序建线段树)
- Qt学习之路---5.线程