概念
Ant表达式是一种在Ant的XML文件中使用的语法,用于表示条件判断、循环、变量替换等操作。它使用${}包围变量名或表达式,以区分普通文本。在Ant表达式中,可以使用各种逻辑运算符(如==、!=、<、>等)、函数调用、属性引用等功能,以实现复杂的逻辑判断和操作。
通配符
?:匹配单个字符
*:匹配0或多个字符
**:匹配0或多个目录
用途
属性引用
通过 ${propertyName} 的形式来引用一个已定义的属性。例如,如果定义了一个属性 version=1.0,那么在其他地方可以使用 ${version} 来获取值 1.0。
文件集模式
Ant 中可以使用表达式来定义文件集的包含和排除模式。常见的通配符如 *(匹配任意字符序列)、?(匹配单个字符)、**(匹配任意个目录)等可以用于构建灵活的文件选择规则。
条件判断
可以在 Ant 任务中使用表达式来进行条件判断,比如根据某个属性的值来决定是否执行特定的操作。
环境变量引用
可以通过 ${env.VARIABLE_NAME} 的形式来引用系统环境变量。
路径操作
可以结合表达式来处理文件路径相关的操作,如构建特定的文件路径等。
示例
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 配置请求地址的权限
.authorizeRequests()
.antMatchers("/test/demo").permitAll() // 所有用户可访问
.antMatchers("/test/admin").hasRole("ADMIN") // 需要 ADMIN 角色
.antMatchers("/test/normal").access("hasRole('ROLE_NORMAL')") // 需要 NORMAL 角色。
// 任何请求,访问的用户都需要经过认证
.anyRequest().authenticated()
.and()
// 设置 Form 表单登陆
.formLogin()
// .loginPage("/login") // 登陆 URL 地址
.permitAll() // 所有用户可访问
.and()
// 配置退出相关
.logout()
// .logoutUrl("/logout") // 退出 URL 地址
.permitAll(); // 所有用户可访问
}
。。。
}
其中antMatchers()方法中的参数使用的就是ant表达式,表达的是接口请求路径的匹配
评论区