十次方第六天[Java项目]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yan12422/article/details/84800350

大家需要十次方项目的视频可以关注我的微信公众号,

加密与微服务JWT

BCrypt加密

说明下这个加密算法,这个是Spring Security提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强哈希方法来加密密码,BCrypt强哈希,每次加密的结果都不一样,

  1. 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  1. 添加配置类

我们在添加了Spring Security的依赖后,学过的小伙伴应该都知道,所有的地址都被控制了,而这里我们只是想用这个算法,所以我们需要一个配置类,配置所有地址都可以匿名访问

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * authorizeRequests    所有security安全配置的开端,表示开始说明需要的权限,必须这么写
         * 需要的权限两部分:第一部分四拦截的路径,第二部分访问路径需要的权限
         * antMatchers表示拦截所有路径,/**所有的意思,permitAll任何权限都可以访问
         * anyRequest任何请求,authenticated认证后才能访问
         * and().csrf().disable()固定写法,表示csrf拦截失效
         */
        http
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }
}

这里是要用到security里的加密类,所有要住进来

@Bean
public BCryptPasswordEncoder encoder(){
	return new BCryptPasswordEncoder();
}

密码的加密

admin.setPassword(encoder.encode(admin.getPassword()));

密码的匹配

encoder.matches(没有加密的密码,加密后的)

jjwt快速入门

引入依赖

<dependency>
    <groupId>io.josnwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.6.0</version>
</dependency>

大家需要十次方项目的视频可以关注我的微信公众号,

猜你喜欢

转载自blog.csdn.net/yan12422/article/details/84800350
今日推荐