1. Error screenshot
Using the Spring Security of the Ruoyi framework to report an error, when initiating a Post request call, it prompts "Request access: /ci/card/testPost, authentication failed, unable to access system resources", but the Get request can be released normally.
2. Error description
2.1 Complete Request Situation
@RestController
@RequestMapping(value = "/ci/card")
@Slf4j
@AllArgsConstructor
public class IndexLibraryController extends BaseController {
@ResponseBody
@GetMapping(value = "/testGet", produces = "application/json")
public String testGet() {
return "123";
}
@ResponseBody
@PostMapping(value = "/testPost", produces = "application/json")
public String testPost() {
return "123";
}
}
@ResponseBody
@PostMapping(value = "/testPost", produces = "application/json")
public String testPost() {
return "123";
}
2.2 Test 1: Use get request and return normally
@ResponseBody
@GetMapping(value = "/testGet", produces = "application/json")
public String testGet() {
return "123";
}
2.3 Test 2: Use post request, abnormal return
@ResponseBody
@PostMapping(value = "/testPost", produces = "application/json")
public String testPost() {
return "123";
}
2.4 Check the error point and locate the code
/**
* 认证失败处理类 返回未授权
*
* @author ruoyi
*/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable
{
private static final long serialVersionUID = -8970718410437077606L;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
throws IOException
{
int code = HttpStatus.UNAUTHORIZED;
String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
}
}
2.5 Check the calling location
/**
* 认证失败处理类
*/
@Autowired
private AuthenticationEntryPointImpl unauthorizedHandler;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// CSRF禁用,因为不使用session
.csrf().disable()
// 认证失败处理类
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
//其他配置略
}
The inspection found that only GET requests are fully released, but post requests are not released.
.antMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/ci/**"
).permitAll()
Three, error resolution
Just increase the release of the post request.
.antMatchers(
HttpMethod.POST,
"/",
"/ci/**"
).permitAll()
The complete Spring Security configuration is as follows
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// CSRF禁用,因为不使用session
.csrf().disable()
// 认证失败处理类
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// 过滤请求
.authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/register", "/captchaImage").anonymous()
.antMatchers(
HttpMethod.GET,
"/",
"/*.html",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/ci/**"
).permitAll()
.antMatchers(
HttpMethod.POST,
"/",
"/ci/**"
).permitAll()
.antMatchers("/common/download**").anonymous()
.antMatchers("/common/download/resource**").anonymous()
.antMatchers("/swagger-ui.html").anonymous()
.antMatchers("/swagger-resources/**").anonymous()
.antMatchers("/webjars/**").anonymous()
.antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous()
.antMatchers("/ObjectLoggerServer/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated()
.and()
.headers().frameOptions().disable();
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
// 添加JWT filter
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// 添加CORS filter
httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
}