呈现白名单路径时不添加java样式表
当我添加WebApplicationConfig类和AuthenticationFilter类时,我不知道如何让我的样式表显示在主页上。我的风格她位于resources/static/css。我当前的错误是net::ERR_太多_重定向
我曾尝试将以下内容添加到我的WebApplicationConfig中,但也没有成功
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("/static/css/");
}
这是我的文件结构。我需要回家。将在主页上显示的参考资料/static/css中的css。
这是我的WebApplicationConfig,它实现了WebMVCConiguer
@Configuration
public class WebApplicationConfig implements WebMvcConfigurer {
// Create spring-managed object to allow the app to access our filter
@Bean
public AuthenticationFilter authenticationFilter() {
return new AuthenticationFilter();
}
// Register the filter with the Spring container
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authenticationFilter());
}
}
这是我的AuthenticationFilter类,它扩展了HandlerInterceptorAdapter
public class AuthenticationFilter extends HandlerInterceptorAdapter {
@Autowired
UserRepository userRepository;
@Autowired
AuthenticationController authenticationController;
private static final List<String> whitelist = Arrays.asList("/login", "/register", "/logout", "/", "/css");
private static boolean isWhitelisted(String path) {
for (String pathRoot : whitelist) {
if (path.equals(pathRoot)) {
return true;
}
}
return false;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws IOException {
if (isWhitelisted(request.getRequestURI())) {
// returning true indicates that the request may proceed
return true;
}
HttpSession session = request.getSession();
User user = authenticationController.getUserFromSession(session);
// The user is logged in
if (user != null) {
return true;
}
// The user is NOT logged in
response.sendRedirect("");
return false;
}
}
共 (0) 个答案