springboot整合shiro后Swagger报404
// SwaggerConfig package com.kaituo.common.swagger; import com.google.common.base.Predicates; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * swagger 接口文档工具 * @Author: RenShuai * @Classname SwaggerConfig * @Package com.kaituo.common.swagger * @Date 2019/8/28 13:48 * @Version V1.0 * @address : http://localhost:7100/swagger-ui.html */ @Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = { "com.kaituo.common.*" }) public class SwaggerConfig implements WebMvcConfigurer { /** * 创建一个Docket对象 * 调用select()方法, * 生成ApiSelectorBuilder对象实例,该对象负责定义外漏的API入口 * 通过使用RequestHandlerSelectors和PathSelectors来提供Predicate,在此我们使用any()方法,将所有API都通过Swagger进行文档管理 * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //标题 .title("科研项目接口文档") //简介 .description("") //服务条款 .termsOfServiceUrl("") //作者个人信息 .contact(new Contact("renshuai","","xxx@xxx.com")) //版本 .version("1.0") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/**").addResourceLocations("classpath*:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); // super.addResourceHandlers(registry); } }
shiro的配置
package com.kaituo.shiro.configs; import com.kaituo.pojo.bo.ShiroBO; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.session.mgt.eis.MemorySessionDAO; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.LinkedHashMap; import java.util.Map; /** * @program: scaffold * @description: * @author: 由苏泽华创建 * @create: 2019-09-29 14:04 **/ @Configuration public class ShiroConfig { @Autowired ShiroBO shiroBO; @Bean @ConditionalOnMissingBean public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator app = new DefaultAdvisorAutoProxyCreator(); app.setProxyTargetClass(true); return app; } @Bean(name = "shiroFilter") public ShiroFilterFactoryBean shiroFilter() { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager()); //登录 // shiroFilterFactoryBean.setLoginUrl("/login"); //未授权界面,听说这个有问题。是个坑 // shiroFilterFactoryBean.setUnauthorizedUrl("/notRole"); Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("classpath:/static/**", "anon"); filterChainDefinitionMap.put("/css/**", "anon"); filterChainDefinitionMap.put("/js/**", "anon"); filterChainDefinitionMap.put("/img/**", "anon"); filterChainDefinitionMap.put("/favicon.ico", "anon"); filterChainDefinitionMap.put("/swagger-ui.html", "anon"); filterChainDefinitionMap.put("/swagger/**","anon"); filterChainDefinitionMap.put("/webjars/**", "anon"); filterChainDefinitionMap.put("/swagger-resources/**","anon"); filterChainDefinitionMap.put("/v2/**","anon"); filterChainDefinitionMap.put("/configuration/**", "anon"); filterChainDefinitionMap.put("/configuration/security", "anon"); filterChainDefinitionMap.put("/configuration/ui", "anon"); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/api/**", "anon"); filterChainDefinitionMap.put("/logout", "anon"); filterChainDefinitionMap.put("/admin/**", "authc"); filterChainDefinitionMap.put("/user/**", "authc"); //主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 剩余的都需要认证 // filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 创建securityManger对象 * * @param * @Description: 创建securityManger对象 * @Param: * @return: org.apache.shiro.web.mgt.DefaultWebSecurityManager * @Author: 苏泽华 * @Date: 2019/1/10 */ @Bean public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setSessionManager(sessionManager()); // TODO: 2019/8/29 // securityManager.setCacheManager(redisCacheManager()); securityManager.setRealm(realm()); return securityManager; } @Bean public DefaultWebSessionManager sessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionDAO(sessionDAO()); sessionManager.setDeleteInvalidSessions(true); sessionManager.setGlobalSessionTimeout(shiroBO.getShiroSessionTimeout()); sessionManager.setSessionValidationInterval(shiroBO.getShiroSessionValidationInterval()); sessionManager.setSessionValidationSchedulerEnabled(true); return sessionManager; } @Bean public MemorySessionDAO sessionDAO() { MemorySessionDAO memorySessionDAO = new MemorySessionDAO(); return memorySessionDAO; } /** * 创建密码验证器 * * @param * @Description: 创建密码验证器 * @Param: * @return: org.apache.shiro.authc.credential.HashedCredentialsMatcher * @Author: 苏泽华 * @Date: 2019/1/10 */ @Bean public HashedCredentialsMatcher credentialsMatcher() { HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName(shiroBO.getHashAlgorithmName()); hashedCredentialsMatcher.setHashIterations(shiroBO.getHashIterations()); return hashedCredentialsMatcher; } /** * * * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证 * * * 配置以下两个bean(DefaultAdvisorAutoProxyCreator(可选)和AuthorizationAttributeSourceAdvisor)即可实现此功能 * * @return */ // @Bean // @DependsOn({"lifecycleBeanPostProcessor"}) // public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { // DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); // advisorAutoProxyCreator.setProxyTargetClass(true); // return advisorAutoProxyCreator; // } @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager()); return authorizationAttributeSourceAdvisor; } /** * 创建realm * * @param * @Description: 创建realm * @Param: * @return: com.company.shiro.realm.TestCustomRealm * @Author: 苏泽华 * @Date: 2019/1/10 */ @Bean public CustomRealm realm() { CustomRealm realm = new CustomRealm(); realm.setShiroBO(shiroBO); realm.setCredentialsMatcher(credentialsMatcher()); return realm; } }
错误信息
已经整了两天了