SpringBoot 401 Unauthorized问题

控制层接口代码:

@Api(tags = "控制图类型")
@RestController
@RequestMapping("/alarm/controlChart")
public class DimControlChartInfoController extends BaseController {
    @Resource
    private IDimControlChartInfoService dimControlChartInfoService;

    /**
     * 查询控制图类型列表
     */
    @ApiOperation("查询控制图类型列表")
    @PreAuthorize("@ss.hasPermi('alarm:controlChart:list')")
    @GetMapping("/list")
    public TableDataInfo list(DimControlChartInfo dimControlChartInfo) {
        startPage();
        List<DimControlChartInfo> list = dimControlChartInfoService.selectDimControlChartInfoList(dimControlChartInfo);
        return getDataTable(list);
    }

访问接口:

{
    "timestamp": "2023-02-22T10:12:59.231+08:00",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/alarm/controlChart/list"
}

定位找到问题存在如下情况:

1:导入了Spring-security包的问题
2Authorization 认证不通过,有些Authorization含有有效时长限制。(token时效问题)

Spring-security依赖包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

当我们使用了Spring-security 之后,Spring Security默认对所有路径进行权限认证,并且提供默认的登陆页面。如果系统最终没有使用到Spring Security,将该依赖移除即可解决问题;如果系统确实需要使用Spring Security,那么可以自定义路径鉴权方式:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequest()              
            // 直接放行
            .antMatchers("/auth/**", "/error/**", "/dev/**").permitAll()
            // 权限认证
            .anyRequest().authenticated();
    }
}


Spring Security默认的configure(HttpSecurity httpSecurity)实际上等同于如下配置:

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequest()
            // 对所有http请求进行权限认证
            .anyRequest().authenticated().and()
            // 支持基于表单的登陆
            .formLogin().and()
            // 支持基于Basic方式的认证
            .httpBasic();
    }

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方大拿拿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值