SpringBoot+Security+JWT 问题求助

雪月弦歌 2020-05-04 09:46:33
问题:Full authentication is required to access this resource

我在使用swagger进行接口测试时,可以正常进入业务处理逻辑。
使用在线http模拟请求时,也可以正常进入业务处理逻辑。
使用vue+vuetify+axios执行请求时,开始报Full authentication is required to access this resource的错误。我没有想通问题在哪?是不是我的vue环境的问题?大佬们指点一下

后台服务和相关
搭建的项目使用的是SpringBoot+Security+JWT进行鉴权授权,前端使用的是vue+vuetify+axios 开发的,后端也设置了跨域。


@Configuration
public class LibvanMvcConfig implements WebMvcConfigurer {

/**
* 跨域访问
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("Content-Type", "X-Requested-With", "accept,Origin", "Access-Control-Request-Method",
"Access-Control-Request-Headers", "token")
.allowedMethods("*").allowedOrigins("*").allowCredentials(true);
}
}



WebSecurityConfigurerAdapter的配置


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class LibvanSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailsService userDetailsService;
@Autowired
LibvanJwtException libvanJwtException;
@Autowired
LibvanAccessException libvanAccessException;

@Override
public void configure(WebSecurity web) throws Exception {
// druid管理控制台,用户注册,用户登录功能和swagger文档放行
web.ignoring().antMatchers( ///////////////////// swagger权限///////////////////
"/swagger-ui.html", "/swagger-resources/**", "/images/**", "/webjars/**", "/v2/api-docs",
///////////////////// druid权限///////////////////
"/configuration/ui", "/configuration/security", "/druid/**",
/////////// 账号注册的权限/////////账号登录的权限////////
"/api/user/register", "/api/user/login");
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// 校验用户账号密码
auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder() {
// 对密码进行加密
@Override
public String encode(CharSequence charSequence) {
StaticLog.info(LibvanSecurityConfig.class.getSimpleName(), charSequence.toString());
return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
}

// 对密码进行判断匹配
@Override
public boolean matches(CharSequence charSequence, String s) {
String encode = DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
boolean res = s.equals(encode);
return res;
}
});
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 错误处理和异常捕获
.exceptionHandling().accessDeniedHandler(libvanAccessException).and().exceptionHandling()
.authenticationEntryPoint(libvanJwtException).and()
// 因为使用JWT,所以不需要HttpSession
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
// 跨域预检
.requestMatchers(CorsUtils::isPreFlightRequest).permitAll().anyRequest().authenticated().and().cors()
.and().csrf().disable();
// 使用自定义的 Token过滤器 验证请求的Token是否合法
http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
http.headers().cacheControl();
}

@Bean
public JwtTokenFilter authenticationTokenFilterBean() throws Exception {
return new JwtTokenFilter();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}



权限不足的错误处理

@Component
public class LibvanAccessException implements AccessDeniedHandler {

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
String message =accessDeniedException.getMessage();
LibvanResponse libvanResponse = new LibvanResponse();
ApiUtils.apiFial(libvanResponse, message);
response.setStatus(200);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.getWriter().println(JSONUtil.parse(libvanResponse));
}

}


...全文
149 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪月弦歌 2020-05-04
  • 打赏
  • 举报
回复
确实如1楼所言,疏忽了,vue代码我把token放到了参数里面了,而鉴权设计是在header里面取token。非常感谢@亦夜,结帖!
亦夜 2020-05-04
  • 打赏
  • 举报
回复
你的请求头里面携带了jwt令牌吗,你的swagger是被忽略了的,你的http测试应该是有携带token的吧,问题很清楚就是访问该资源需要认证,security会拦截所有请求进行认证的,你可以断点看一下,vue的请求在哪一步被拒绝了,这种问题代码是很难看出来的

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧