springBoot集成token认证

hutool-all

5.4.4

注意:我们默认是已经配置好mybatis的web项目,还未配置myBatis?点击此处

2.自定义两个注解


用来跳过验证的PassToken

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface PassToken {

boolean required() default true;

}

需要登录才能进行操作的注解UserLoginToken

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface UserLoginToken {

boolean required() default true;

}

@Target:注解的作用目标

  • @Target(ElementType.TYPE)——接口、类、枚举、注解

  • @Target(ElementType.FIELD)——字段、枚举的常量

  • @Target(ElementType.METHOD)——方法

  • @Target(ElementType.PARAMETER)——方法参数

  • @Target(ElementType.CONSTRUCTOR) ——构造函数

  • @Target(ElementType.LOCAL_VARIABLE)——局部变量

  • @Target(ElementType.ANNOTATION_TYPE)——注解

  • @Target(ElementType.PACKAGE)——包

@Retention:注解的保留位置

  • RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。

  • RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。

  • RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。

  • @Document:说明该注解将被包含在javadoc中

  • @Inherited:说明子类可以继承父类中的该注解

在这里插入图片描述

3.简单自定义一个实体类User


public class User implements java.io.Serializable {

/** 版本号 */

private static final long serialVersionUID = 6111899065812654266L;

/** 主键自增Id */

private Integer userId;

/** 用户民 */

private String userName;

/** 用户密码 */

private String password;

/**

  • 获取主键自增Id

  • @return 主键自增Id

*/

public Integer getUserId() {

return this.userId;

}

/**

  • 设置主键自增Id

  • @param userId

  •      主键自增Id
    

*/

public void setUserId(Integer userId) {

this.userId = userId;

}

/**

  • 获取用户民

  • @return 用户民

*/

public String getUserName() {

return this.userName;

}

/**

  • 设置用户民

  • @param userName

  •      用户民
    

*/

public void setUserName(String userName) {

this.userName = userName;

}

/**

  • 获取用户密码

  • @return 用户密码

*/

public String getPassword() {

return this.password;

}

/**

  • 设置用户密码

  • @param password

  •      用户密码
    

*/

<
  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 SpringBoot 集成 tokenJava 代码示例: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired private JwtAuthenticationEntryPoint unauthorizedHandler; @Bean public JwtAuthenticationFilter authenticationTokenFilterBean() throws Exception { return new JwtAuthenticationFilter(); } @Override public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { authenticationManagerBuilder .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Bean(BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http .cors() .and() .csrf() .disable() .exceptionHandling() .authenticationEntryPoint(unauthorizedHandler) .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers("/api/auth/**") .permitAll() .anyRequest() .authenticated(); // 添加 JWT filter http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); } } ``` 这段代码使用了 Spring Security 和 JWT 实现了 token 认证。其中,`JwtAuthenticationFilter` 是一个自定义的过滤器,用于解析和验证 JWT token。在 `configure` 方法中,我们配置了哪些请求需要认证,哪些请求不需要认证

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值