zoukankan      html  css  js  c++  java
  • Spring Security 自定义配置(1)

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        //ip认证者配置
        @Bean
        IpAuthenticationProvider ipAuthenticationProvider() {
            return new IpAuthenticationProvider();
        }
    
        //配置封装ipAuthenticationToken的过滤器
        IpAuthenticationProcessingFilter ipAuthenticationProcessingFilter(AuthenticationManager authenticationManager) {
            IpAuthenticationProcessingFilter ipAuthenticationProcessingFilter = new IpAuthenticationProcessingFilter();
            //为过滤器添加认证器
            ipAuthenticationProcessingFilter.setAuthenticationManager(authenticationManager);
            //重写认证失败时的跳转页面
            ipAuthenticationProcessingFilter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler("/ipLogin?error"));
            return ipAuthenticationProcessingFilter;
        }
    
        //配置登录端点
        @Bean
        LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint(){
            LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint
                    ("/ipLogin");
            return loginUrlAuthenticationEntryPoint;
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .antMatchers("/ipLogin").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .logout()
                    .logoutSuccessUrl("/")
                    .permitAll()
                    .and()
                .exceptionHandling()
                    .accessDeniedPage("/ipLogin")
                    .authenticationEntryPoint(loginUrlAuthenticationEntryPoint())
            ;
    
            //注册IpAuthenticationProcessingFilter  注意放置的顺序 这很关键
            http.addFilterBefore(ipAuthenticationProcessingFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
    
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(ipAuthenticationProvider());
        }
    
    }
    
  • 相关阅读:
    GO异常 | runnerw.exe: CreateProcess failed with error 21
    tab切换效果
    第一次码前端页面的经历
    开博第一天
    软件架构参考模板-软件架构设计学习第三天(原创)
    架构设计基础-软件架构设计学习第二天(非原创)
    IT架构师介绍-软件架构设计学习第一天(非原创)
    Android常见面试题学习第二天(原创)
    Android常见面试题学习第一天(原创)
    Netty简单介绍(非原创)
  • 原文地址:https://www.cnblogs.com/leihuazhe/p/7846266.html
Copyright © 2011-2022 走看看