【问题标题】:spring security + spring boot mvc index page before authentication looks like bare html认证前的spring security + spring boot mvc索引页面看起来像裸html
【发布时间】:2019-11-21 04:40:18
【问题描述】:

授权前

我使用任何帐户登录后

有谁知道怎么回事?没有线索。

【问题讨论】:

  • 欢迎来到stackoverflow。三是目前信息太少无法给大家解答。请添加上述页面以及您的配置。请将它们添加为文本并使用正确的代码标签。

标签: spring model-view-controller spring-security


【解决方案1】:

似乎您的 css 没有为不安全的上下文加载。要添加资源,请在 WebMvcConfigurer 中创建如下资源处理程序条目

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern(URL_PATTERN_UICONTENT)) {
            registry.addResourceHandler(URL_PATTERN_UICONTENT).addResourceLocations("classpath:/uicontent/").setCachePeriod(31556926);
            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        }
    }

【讨论】:

  • 我创建了新类 WebMVCConfig 扩展了 WebMvcConfigurerAdapter。它说它已弃用扩展。我应该把这段代码放在哪里?
  • 您的新类应该“实现 WebMvcConfigurer”而不是“扩展 WebMvcConfigurerAdapter”。另外我相信您已经配置了模板引擎并按预期放置了静态资源。
  • 如果我 impelement WebMvcConfigurerAdapter 没有类,但是有 WebMvcConfigurer。可能是你的意思吗?但我也添加了这个类,试图使用你的代码。但是我在控制器中的映射不起作用,我无法访问任何页面。资源/静态 - CSS。资源/模板 - 我的 themyleaf 页面。
  • 是的,我的意思是“WebMvcConfigurer”。你能分享你的 HTML 文件和项目结构吗?
  • 感谢 git repo,我正在努力,很快就会提供解决方案
【解决方案2】:

问题已解决!

我创建了类

@Configuration
@EnableWebMvc
public class WebMVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

这样的话,我说/static/**如果有这个前缀的任何页面请求,所以尝试在classpath中查找:static

在我的索引 thymeleaf 文件中,我包括外部 css 文件:

<link href="../static/css/bootstrap.min.css" th:href="@{static/css/bootstrap.min.css}" rel="stylesheet" />

上面的这个例子展示了如何在应用程序应该找到静态资源的地方添加路由,但这实际上在我的项目中不是必需的。

问题是当你在登录之前尝试访问静态资源时,你应该在 WebSecurityConfig 类中授予它的权限,如下所示:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/index", "/login","/css/**", "/webjars/**", "/images/**", "/js/**").permitAll()
                .antMatchers("/subscribers", "/calldetails").access("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
                .antMatchers("/divisions").access("hasAnyRole('ROLE_ADMIN')")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout")
                .permitAll();

【讨论】:

  • 很高兴你成功了。我打对了?。这是我错过添加的注释
猜你喜欢
  • 2019-03-25
  • 2023-04-10
  • 2017-01-27
  • 1970-01-01
  • 2020-04-26
  • 2014-01-19
  • 2021-03-20
  • 2020-05-26
  • 2020-11-14
相关资源
最近更新 更多