繁体   English   中英

如何使用 Spring Security 修复对资源的访问?

[英]How to fix access to resources using spring security?

我刚刚从 web 复制了带有 Spring Security 的登录/注册表单,但我遇到了大问题。 我想让所有人都公开所有资源,因为 5 个小时后,我完全不知道这个春季安全发生了什么。

好的,这是我的配置方法1:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
...
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            // URLs matching for access rights
            .antMatchers("/").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/register").permitAll()
            .antMatchers("/DBDesign").permitAll()
            .antMatchers("/index").permitAll()
            .antMatchers("/admin/**").hasAuthority("ADMIN")
            .antMatchers("/user/**").hasAuthority("USER")
            .anyRequest().authenticated()
            .and()
            // form login
            .csrf().disable().formLogin()
            .loginPage("/login")
            .failureUrl("/login?error=true")
            .successHandler(sucessHandler)
            .usernameParameter("email")
            .passwordParameter("password")
            .and()
            // logout
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/").and()
            .exceptionHandling()
            .accessDeniedPage("/access-denied");
}

配置方法二:

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/static/**", "/common/**", "/js/**", "/images/**");
    }
}

配置方法三:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/webjars/**", "/static/**", "/templates/**")
                .addResourceLocations("/webjars/", "classpath:/static/", "classpath:/templates/");
    }
}

我 100% 确信这些方法之一负责管理登录前可用和不可用的目录。 伙计们,有人可以解释一下这是如何工作的吗? 看这是我的临时文件结构:

在此处输入图片说明

绿色 --- 我可以访问

红色 --- 我没有访问权限

我可以将路径复制并粘贴到“绿色”文件并查看其中的内容,但是如果我尝试对红色文件执行相同的操作...错误 404。这怎么可能? 只有那些 2x 没有权限。

资源处理程序/static/**指向classpath:/static/

所以安全过滤器应该忽略对/sidebar/**请求,....

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/sidebar/**","/diagramER/**", .....);
    }

然后你可以在你的页面中使用类似的东西

<html lang="en">
<head>
    ....
    <script src="/sidebar/js/main.js" ></script>
    <script src="/diagramER/DBDesign.js" ></script>
    <link rel="stylesheet" type="text/css" href="/sidebar/common/style.css">
</head>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM