简体   繁体   English

Spring 安全和() function

[英]Spring security and() function

Can you please explain in a simple way for what reason we need to use and() method in HttpSecurity.您能否简单解释一下为什么我们需要在 HttpSecurity 中使用and()方法。

Code:代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/").hasAnyRole("Employee", "Manager", "HR")
            .antMatchers("/hr_info").hasRole("HR")
            .antMatchers("/manager_info/**").hasRole("Manager")
            .and().formLogin().permitAll();
}

The spring security reference explains it as follows. spring 安全参考解释如下。

The Java Configuration equivalent of closing an XML tag is expressed using the and() method which allows us to continue configuring the parent. Java 配置等效于关闭 XML 标记使用 and() 方法表示,该方法允许我们继续配置父标记。 If you read the code it also makes sense.如果您阅读代码,它也很有意义。 I want to configure authorized requests and configure form login and configure HTTP Basic authentication.我要配置授权请求,配置表单登录,配置HTTP基本认证。

Accordingly , you say the following with your own settings:因此,您使用自己的设置说出以下内容:

  • Ensures that any request to our application requires the user to be authenticated,确保对我们应用程序的任何请求都需要对用户进行身份验证,
  • Allows authentication for some links only to the specified role,允许仅对指定角色的某些链接进行身份验证,
  • Allows users to authenticate with form based login.允许用户使用基于表单的登录进行身份验证。

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

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