修改sa-token检查token的报错信息

发布时间 2023-12-01 16:11:25作者: xiaobaibao

1.创建一个类去实现 NotLoginExceptionMapper

import cn.dev33.satoken.exception.NotLoginException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype
@Component
public class CustomNotLoginExceptionMapper implements NotLoginExceptionMapper {

@Override
public ResponseEntity<Object> toResponse(NotLoginException e) {
// 自定义处理逻辑,例如返回自定义的错误信息或进行其他操作
String errorMessage = "您没有登录";
return new ResponseEntity<>(errorMessage, HttpStatus.UNAUTHORIZED);
}
}

2.启动类添加注解确保自定义的类可以被扫描到

@ComponentScan("com.example") // com.example替换为上面那个类的包路径

3.将之作为bean在启动类注册

@SpringBootApplication
@ComponentScan("com.example")
public class YourApplication {

public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}

@Bean
public CustomNotLoginExceptionMapper customNotLoginExceptionMapper() {
return new CustomNotLoginExceptionMapper();
}

}