SpringSecurity自定义注解放行,以及在微服务架构中使用

SpringSecurity自定义注解放行,以及在微服务架构中使用

SpringSecurity的认证流程:(个人理解)

Spring Security通过一系列的过滤器组成的过滤链来处理安全相关的任务。在Web应用中,过滤器链主要用于实现身份验证、授权、记住我(Remember Me)等安全功能。

请求先到AuthenticationFilter,首先先验证使用的什么协议(只允许post请求),再获取账号密码,将账号密码封装成authentication,通常是封装成UsernamePasswordAuthenticationToken。注意此处有多种过滤器,BasicAuthenticationFilter,UsernamePasswordAuthenticationFilter,RememberMeAuthenticationFilter,SocialAuthenticationFilter,Oauth2AuthenticationProcessingFilter和Oauth2ClientAuthenticationProcessingFilter只有其中一个认证通过就会封装authentication对象并返回。

然后调用authenticationManager中的authentication方法进行认证,认证成功返回authentication,认证失败AuthenticationManager会根据不同的认证方式选择对应的Provider进行认证。

providerManage实现了AuthenticationManage的多种方法,通过调用其中的DaoAuthenticationprovider方法根据用户名加载用户信息,通过userDetail进行接收后封装为authentication对象并依次返回,返回到AuthenticationFilter时,通过AuthenticationManage进行认证,最后将主题信息返回到security的上下文中(就是保存Authentication对象),下次请求来的时候在securityContextPersistenceFilter中将Authentication拿出来,后续认证就不需要了。

下面说一下SpringSecurity自定义注解放行接口。

应用场景:实际项目开发中,会遇到需要放行一些接口,使其能匿名访问的业务需求。但是每当需要当需要放行时,都需要在security的配置类中进行修改,例如

//                .antMatchers("captcha/check").anonymous()

感觉非常的不优雅。所以想通过自定义一个注解,来进行接口匿名访问。

首先创建一个自定义注解:

@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented //生成文档
public @interface IgnoreAuth {
}

然后编写securityConfig类继承WebSecurityConfigurerAdapter:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return  new BCryptPasswordEncoder();
    }

    @Autowired
    private JwtAuthenticationTokenFilter filter;

    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;


    @Resource
    private AccessDeniedHandler accessDeniedHandler;

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



        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()

                // 对于登录接口 允许匿名访问 未登录状态也可以访问

                .antMatchers("/token/refreshToken").anonymous()
//                 需要用户带有管理员权限
                .antMatchers("/find").hasRole("管理员")
                // 需要用户具备这个接口的权限
                .antMatchers("/find").hasAuthority("menu:user")

                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        //添加过滤器
        http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);

        //配置异常处理器
        http.exceptionHandling()
                //配置认证失败处理器
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    /**
     * @ description: 使用这种方式放行的接口,不走 Spring Security 过滤器链,
     *                无法通过 SecurityContextHolder 获取到登录用户信息的,
     *                因为它一开始没经过 SecurityContextPersistenceFilter 过滤器链。
     * @ dateTime: 2021/7/19 10:22
     */
    

}

自定义注解实现

说明:下面两种放行方式不能在有@ResquestMapper注解的接口上面使用,只能在@GetMapper,@PostMapper的接口中使用,因为我是通过请求方式进行放行的。

SpringSecurity提供了两种放行方式:

1.使用这种方式放行的接口,不走 Spring Security 过滤器链,

public void configure(WebSecurity web)

2.使用这种方式放行的接口,走 Spring Security 过滤器链,

protected void configure(HttpSecurity http) 

1.使用走 Spring Security 过滤器链的放行方式

RequestMappingHandlerMapping组件可以获取系统中的所有接口,如图

在这里插入图片描述

我们可以对此进行遍历,获取携带了@IgnoreAuth的接口,再通过接口的请求方式进行放行

Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
System.out.println("handlerMethods:" + handlerMethods);
handlerMethods.forEach((info, method) -> {
    if (info.getMethodsCondition().getMethods().size() != 0) {

        // 带IgnoreAuth注解的方法直接放行
        if (!Objects.isNull(method.getMethodAnnotation(IgnoreAuth.class))) {
            // 根据请求类型做不同的处理
            info.getMethodsCondition().getMethods().forEach(requestMethod -> {
                switch (requestMethod) {
                    case GET:
                        // getPatternsCondition得到请求url数组,遍历处理
                        info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                            // 放行
                            try {
                                http.authorizeRequests()
                                        .antMatchers(HttpMethod.GET, pattern.getPatternString())
                                        .anonymous();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        });
                        break;
                    case POST:
                        info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                            try {
                                http.authorizeRequests()
                                        .antMatchers(HttpMethod.POST, pattern.getPatternString())
                                        .anonymous();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        });
                        break;
                    case DELETE:
                        info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                            try {
                                http.authorizeRequests()
                                        .antMatchers(HttpMethod.DELETE, pattern.getPatternString())
                                        .anonymous();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        });
                        break;
                    case PUT:
                        info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                            try {
                                http.authorizeRequests()
                                        .antMatchers(HttpMethod.PUT, pattern.getPatternString())
                                        .anonymous();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        });
                        break;
                    default:
                        break;
                }
            });
        }
    }
});

需要注意的是,此处可能由于版本的不同,获取请求名称的方式有所不同。

在这里插入图片描述

这里通过pathPatternsCondition进行获取,某些版本需要在patternsCondition中进行获取,具体看个人的版本情况。

1.使用不走 Spring Security 过滤器链的放行方式

代码大体相同,都是首先获取所有接口,再进行遍历放行

        WebSecurity and = web.ignoring().and();
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
//        System.out.println("handlerMethods:" + handlerMethods);

        handlerMethods.forEach((info, method) -> {
            if (info.getMethodsCondition().getMethods().size() != 0) {

                // 带IgnoreAuth注解的方法直接放行
                if (!Objects.isNull(method.getMethodAnnotation(IgnoreAuth.class))) {
                    // 根据请求类型做不同的处理
                    info.getMethodsCondition().getMethods().forEach(requestMethod -> {
                        switch (requestMethod) {
                            case GET:
                                // getPatternsCondition得到请求url数组,遍历处理
                                info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                                    // 放行
                                    and.ignoring().antMatchers(HttpMethod.GET, pattern.getPatternString());

                                });
                                break;
                            case POST:
                                info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                                    and.ignoring().antMatchers(HttpMethod.POST,  pattern.getPatternString());
                                });
                                break;
                            case DELETE:
                                info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                                    and.ignoring().antMatchers(HttpMethod.DELETE,  pattern.getPatternString());
                                });
                                break;
                            case PUT:
                                info.getPathPatternsCondition().getPatterns().forEach(pattern -> {
                                    and.ignoring().antMatchers(HttpMethod.PUT,  pattern.getPatternString());
                                });
                                break;
                            default:
                                break;
                        }
                    });
                }
            }
        });

    }

在微服务架构中进行使用:

在微服务中,我们需要在不同的模块中实现单点登录,或使用到SpingSecurity的认证鉴权,或使用自己定义的放行注解。下面是我的解决方式。

项目模块为:

在这里插入图片描述

在springSecurity模块中配置了Jwt登录拦截,Redis,SpringSecurity配置等。

在这里插入图片描述

然后只需要在user-center中引入这个模块,这样就可以在user-center中使用配置好的功能。

在不同的模块中实现单点登录,或使用到SpingSecurity的认证鉴权,或使用自己定义的放行注解。下面是我的解决方式。

项目模块为:

[外链图片转存中…(img-VbrUll2N-1697356283528)]

在springSecurity模块中配置了Jwt登录拦截,Redis,SpringSecurity配置等。

[外链图片转存中…(img-AXwA9Aay-1697356283528)]

然后只需要在user-center中引入这个模块,这样就可以在user-center中使用配置好的功能。

在这里插入图片描述
文章仅为个人理解,欢迎指正。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Security是一个在Java应用程序提供身份验证和授权的框架。它通过使用各种身份验证和授权技术,帮助开发人员实现应用程序的安全性。 在Spring Security,身份验证包括验证用户的身份以确保其是合法的,并且授权包括确定用户是否有权访问特定功能或资源。以下是Spring Security的一些关键概念和用法: 1. 身份验证:Spring Security提供了许多身份验证机制,例如基于表单的身份验证、基于HTTP基本身份验证、基于LDAP的身份验证等。开发人员可以选择适合他们应用程序需求的身份验证机制。 2. 授权:Spring Security使用许可(Permission)和角色(Role)的概念来控制访问权限。可以使用特定的注解或编程方式将这些权限和角色应用到方法或URL上。 3. 认证和授权流程:Spring Security在认证和授权过程使用了一系列的过滤器和提供者。它们分别负责处理身份验证和授权的不同方面。开发人员可以根据需要定制这些组件来满足自己的应用程序需求。 4. AccessDecisionManager:这是Spring Security的一个重要组件,用于决定用户是否有权限访问特定的资源或功能。开发人员可以实现自己的AccessDecisionManager来根据自己的逻辑进行权限决策。 5. UserDetails:在Spring Security,用户信息通过UserDetails接口进行封装。开发人员可以根据自己的需求实现自定义的UserDetails接口,并提供用户的身份验证和授权信息。 6. 匿名认证:Spring Security支持为匿名用户建立一个匿名Authentication对象。这样,无需再对匿名用户进行额外的验证,可以直接将其当作正常的Authentication对象来使用。 综上所述,Spring Security提供了全面的身份验证和授权机制来保护应用程序的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值