zoukankan      html  css  js  c++  java
  • Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web

    @Configuration
    @EnableWebMvcSecurity
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Autowired
        private AuthenticationProvider authenticationProvider;
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(authenticationProvider);
        }
    
        @Configuration
        @Order(1)
        public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable()
                        .antMatcher("/api/**")
                        .authorizeRequests()
                            .anyRequest().hasAnyRole("ADMIN", "API")
                            .and()
                        .httpBasic();
            }
        }
    
        @Configuration
        @Order(2)
        public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
    
            @Override
            public void configure(WebSecurity web) throws Exception {
                web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable() //HTTP with Disable CSRF
                        .authorizeRequests() //Authorize Request Configuration
                            .antMatchers("/connect/**").permitAll()
                            .antMatchers("/", "/register").permitAll()
                            .antMatchers("/admin/**").hasRole("ADMIN")
                            .anyRequest().authenticated()
                            .and() //Login Form configuration for all others
                        .formLogin()
                            .loginPage("/login").permitAll()
                            .and() //Logout Form configuration
                        .logout().permitAll();
            }
        }
    }

    http://stackoverflow.com/questions/27774742/spring-security-http-basic-for-restful-and-formlogin-cookies-for-web-annotat/27833237#27833237

  • 相关阅读:
    Python学习之collections module-defaultdict()
    LinkList Operation
    Eng1—English daily notes
    (知识扩展)R运用领域一览表
    TED_Topic3:The hidden reason for poverty the world needs to address now
    Stat3—因子分析(Factor Analysis)
    R3—日期处理
    Stat2—主成分分析(Principal components analysis)
    TED_Topic2:My desperate journey with a human smuggler
    MagicB.0—怎样设置电脑自动关机?
  • 原文地址:https://www.cnblogs.com/softidea/p/6229553.html
Copyright © 2011-2022 走看看