方法
一、前端
router.js文件添加路由 Vue.use(Router)
export const constantRouterMap = [ { path: ‘/lims’, meta: { title: ‘发热量数据页’, noCache: true }, component: () => import(’@/views/lims/heat/index’), hidden: true }, ] index.js文件中添加白名单 const whiteList = [’/login’, ‘/tong’, ‘/lims’]// no redirect whitelist
二、后端
1.修改控制器
HeatController.java文件中给方法添加注解
@GetMapping @Log(“查询发热量接口”) @ApiOperation(“查询发热量接口”) //@GetMapping(value = “/heat”) @AnonymousAccess//注意要添加这个注解 //@PreAuthorize("@el.check(‘heat:list’)") public ResponseEntity getHeats(HeatQueryCriteria criteria, Pageable pageable){ return new ResponseEntity<>(heatService.queryAll(criteria,pageable),HttpStatus.OK); //return “Hello World”; } 不添加@AnonymousAccess这个注解会给前端返回401错误。 注释掉@PreAuthorize("@el.check(‘heat:list’)")。
2.修改配置文件SecurityConfig.javat
在方法configure中添加 .antMatchers("/lims/**").permitAll()开发匿名访问。
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
if(true){
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods();
Set<String> anonymousUrls = new HashSet<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> infoEntry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = infoEntry.getValue();
AnonymousAccess anonymousAccess = handlerMethod.getMethodAnnotation(AnonymousAccess.class);
if (null != anonymousAccess) {
anonymousUrls.addAll(infoEntry.getKey().getPatternsCondition().getPatterns());
}
}
httpSecurity
.csrf().disable()
.addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationErrorHandler)
.accessDeniedHandler(jwtAccessDeniedHandler)
.and()
.headers()
.frameOptions()
.disable()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(
HttpMethod.GET,
"/*.html",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/webSocket/**"
).permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/*/api-docs").permitAll()
.antMatchers("/avatar/**").permitAll()
.antMatchers("/file/**").permitAll()
.antMatchers("/druid/**").permitAll()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(anonymousUrls.toArray(new String[0])).permitAll()
.antMatchers("/lims/**").permitAll()
.and().apply(securityConfigurerAdapter());
}
总结
|