美文网首页
spring boot + spring security 静态

spring boot + spring security 静态

作者: 偷得浮生半日咸 | 来源:发表于2018-12-28 10:34 被阅读0次

    问题1

    问题描述

    整合spring boot 和 spring security 做用户登录时,使用bootstrap.min.css中的样式,在登录之前页面无法加载样式。


    css文件位置

    解决

    方法1

    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Bean
        UserDetailsService customUserService() {
    
            return new CustomUserService();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception{
            auth.userDetailsService(customUserService());
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login")
                    .failureForwardUrl("/login?error")
                    .permitAll()
                    .and()
                    .logout().permitAll();
        }
        @Override
        public void configure(WebSecurity web) throws Exception {
            //解决静态资源被拦截的问题
            web.ignoring().antMatchers("/css/**");
        }
    
    
    
    }
    

    方法2:

    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Bean
        UserDetailsService customUserService() {
    
            return new CustomUserService();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception{
            auth.userDetailsService(customUserService());
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/css/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login")
                    .failureForwardUrl("/login?error")
                    .permitAll()
                    .and()
                    .logout().permitAll();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:spring boot + spring security 静态

          本文链接:https://www.haomeiwen.com/subject/amfslqtx.html