繁体   English   中英

Spring Security HTTP FormLogin

[英]Spring Security HTTP FormLogin

我的Spring Security有问题

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UsuarioService usuarioService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // @formatter:off

        http
                .authorizeRequests()
                    .antMatchers(
                            "/",
                            "/index/**",
                            "/register",
                            "/doregister",
                            "/login",
                            "/dologin",
                            "/logout-success",
                            "/confirmregister",
                            "/invaliduser",
                            "/expiredtoken",
                            "/userstatuslog",
                            "/verifyemail")
                    .permitAll()
                        .anyRequest()
                            .authenticated()
            .and()
                .authorizeRequests()
                    .antMatchers(
                            "/login",
                            "logout")
                    .permitAll()
            .and()
                .authorizeRequests()
                    .antMatchers(
                            "/static/css/**",
                            "/css/custom.css",
                            "/js/**",
                            "/images/**"
                            )
                        .permitAll()
            .and()
                .authorizeRequests()
                    .antMatchers(
                            "/forbidden",
                            "/edit-profile-about",
                            "/doeditprofileabout",                      
                            "/profile"
                            )
                    .hasRole("USER")
            .and()
                .authorizeRequests()
                    .antMatchers(
                            "/controlpanel",
                            "/forbidden",
                            "/edit-profile-about",
                            "/doeditprofileabout",                      
                            "/profile"
                            )
                    .hasRole("ADMIN")
            .and()
                .formLogin()
                    .loginPage("/login")
                        .defaultSuccessUrl("/")
                            .permitAll()
            .and()
                .logout()
                    .deleteCookies("remove")
                        .invalidateHttpSession(true)
                            .logoutUrl("/logout")
                .logoutSuccessUrl("/logout-success")
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));

        // @formatter:on
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(usuarioService).passwordEncoder(passwordEncoder);
    }

每次登录时,它会将我发送到引导文件...

本地主机:8080 / css / bootstrap-3.3.7-dist / css / bootstrap.min.css

要么...

本地主机:8080 / css / custom.css

要么...

本地主机:8080 / js / jquery-2.1.4.min.js

但不是同一个人(似乎是随机的!:-)

然后,有时CSS有效,有时却无效。 在登录表单中,它没有样式,但是只有某些时候,因为在我运行应用程序时,它具有样式。 当我也登录时,但在注销或尝试再次登录时却没有。 因此,当用户正确登录后,它将再次具有样式。

并且当我尝试与用户(使用ROLE_USER)访问“ / controlpanel”时,它允许访问(并且不应),但是当我与用户(Role_ADMIN)一起使用时,它可以正常工作。

问题是来宾无法访问您的css文件。 因此,登录后,您将被重定向回它。 由于浏览器缓存,登录页面中的CSS有时会起作用。

尝试将您的配置更改为

http
    .authorizeRequests()
        .antMatchers(
                "/",
                "/index/**",
                "/register",
                ...).permitAll()
        .antMatchers(
                "/login",
                "logout").permitAll()
        .antMatchers(            // You need to add all css file here
                "/static/css/**",
                "/css/custom.css",
                "/css/bootstrap-3.3.7-dist/css/bootstrap.min.cs‌​‌​‌​s",
                "/js/**",
                "/images/**").permitAll()
        .antMatchers(
                "/forbidden",
                "/edit-profile-about",
                "/doeditprofileabout",                      
                "/profile").hasRole("USER")
        .antMatchers(
                "/controlpanel",
                "/forbidden",
                "/edit-profile-about",
                "/doeditprofileabout",                      
                "/profile").hasRole("ADMIN")
        .anyRequest().authenticated()
    .and()
        ...

当Spring Security过滤了您的静态资源时,就会发生这种情况。

尝试完全忽略这些资源:

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

定义模式以满足您的所有位置。

暂无
暂无

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

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