异常:There was an unexpected error (type=Unauthorized, status=401).解决办法

运行springboot的时候报错,其他配置都没有问题,其他页面可以正常访问,进入这个页面时报错。

一、错误提示:

异常:There was an unexpected error (type=Unauthorized, status=401).解决办法_第1张图片

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Aug 07 10:44:17 CST 2021

There was an unexpected error (type=Unauthorized, status=401).

 二、原因:

使用了拦截器,没有给想要访问的页面授权,所以阻止了用户的访问。

三、解决方法:

1、找到所设置的包config

异常:There was an unexpected error (type=Unauthorized, status=401).解决办法_第2张图片

 2、找到授权的设置 : filterMap.put("/user/add","perms[user:add]");

异常:There was an unexpected error (type=Unauthorized, status=401).解决办法_第3张图片

 

package com.iss.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @BelongsProject: shiro-springboot
 * @BelongsPackage: com.iss.config
 * @Author: yanhongwei
 * @CreateTime: 2021-08-06  17:46
 * @Description:
 */
@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean:3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        //添加shiro的内置过滤器
        /**
         * anon:无需认证就可以访问
         * authc:必須认证了才能访问
         * user: 必须拥有记住我功能才能用
         * perms: 拥有对某个资源的权限才能访问
         * role:拥有某个角色权限才能访问
         */
//        拦截
    Map filterMap = new LinkedHashMap<>();

//        授权
        filterMap.put("/user/add","perms[user:add]");
                filterMap.put("/user/update","authc");
                bean.setFilterChainDefinitionMap(filterMap);

                //如果没有权限设置登录请求
                bean.setLoginUrl("/toLogin");
                return bean;

                }

//DafaultWebSecurityManager:2
@Bean(name = "securityManager")
public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
        }

//创建realm对象 需要自定义: 1
@Bean
public UserRealm userRealm() {
        return new UserRealm();
        }
}

      

3、将代码修改为:

 // 授权
        filterMap.put("/user/add", "authc");
        filterMap.put("/user/update", "authc");
        bean.setFilterChainDefinitionMap(filterMap);

4、重新运行代码

异常:There was an unexpected error (type=Unauthorized, status=401).解决办法_第4张图片

5、实现效果 

异常:There was an unexpected error (type=Unauthorized, status=401).解决办法_第5张图片

 

你可能感兴趣的:(spring,intellij-idea,maven,java)