若依设置匿名访问遇到的问题
问题:给模块详情管理/website/details/{{id}
设置白名单不成功
原因:@PreAuthorize
注解没有注释;是因为@PreAuthorize
是Spring Security的权限校验注解。它会在方法执行前判断当前用户是否有指定权限。如果没有权限(比如未登录的匿名用户),会直接拒绝访问,返回未授权(通常是401或403)。
优先级问题
Spring Security 的AOP拦截顺序是:
● 先进行认证(是否登录、token校验等)
● 再进行授权(@PreAuthorize等权限注解)
也就是说,即使你允许匿名访问(通过@Anonymous
或白名单),但如果方法上有@PreAuthorize
注解,Spring Security依然会走权限校验逻辑。对于未登录用户,@PreAuthorize("@ss.hasPermi('xxx')")
里的 @ss.hasPermi
方法一般会返回false,导致匿名用户被拒绝访问。
解决方案:@PreAuthorize
注释就好
/**
* 获取模块详情管理详细信息
*/
// @PreAuthorize("@ss.hasPermi('website:details:query')")
@Anonymous
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Integer id)
{
return success(ntsContentDetailsService.selectNtsContentDetailsById(id));
}