Spring Security是一个可以为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组能够在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减小了为企业系统安全控制编写大量重复代码的工做。html
在Spring Security源码分析十三:Spring Security 基于表达式的权限控制中,咱们只是在后台增长了权限控制,并未在页面作任何处理,与之对应的按钮和连接仍是会显示在页面上,用户体验较差。本章使用Spring Security
标签库来包裹须要保护的内容。java
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
复制代码
public class ClassPathTldsLoader {
private static final String SECURITY_TLD = "/META-INF/security.tld";
final private List<String> classPathTlds;
public ClassPathTldsLoader(String... classPathTlds) {
super();
if(classPathTlds.length == 0){
this.classPathTlds = Arrays.asList(SECURITY_TLD);
}else{
this.classPathTlds = Arrays.asList(classPathTlds);
}
}
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@PostConstruct
public void loadClassPathTlds() {
freeMarkerConfigurer.getTaglibFactory().setClasspathTlds(classPathTlds);
}
}
复制代码
@Bean
@ConditionalOnMissingBean(ClassPathTldsLoader.class)
public ClassPathTldsLoader classPathTldsLoader(){
return new ClassPathTldsLoader();
}
复制代码
<!-- 引入标签-->
<#assign sec=JspTaglibs["http://www.springframework.org/security/tags"] />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body align="center">
<h2>Spring Security Demo</h2>
<!-- freemarker使用security标签格式以下-->
<@sec.authorize access="hasRole('ROLE_ADMIN')">
you can see this
</@sec.authorize>
<@sec.authorize access="isAnonymous()">
you can see isAnonymous
</@sec.authorize>
</body>
</html>
复制代码
authorize
用于判断用户是否具备对应权限,从而控制其限制的内容,包含如下属性。git
access
属性须要使用表达式来判断权限,当表达式的返回结果为true时表示拥有对应的权限。github
<@sec.authorize access="hasRole('ROLE_ADMIN')">
此内容仅对在授予权限列表中拥有“ROLE_ADMIN”权限的用户可见
</@sec.authorize>
--------------------------
<@sec.authorize access="hasPermission(#domain,'read') or hasPermission(#domain,'write')">
只有具备读取或写入权限的用户才能看到此内容,该用户被发现为名为“domain”的请求属性。
</@sec.authorize>
-----------------------------
<@sec.authorize url="/admin">
此内容仅对有权将请求发送到“/ admin”连接的用户可见
</@sec.authorize>
复制代码
authentication
标签用来表明当前Authentication
对象,主要用于获取当前Authentication
的相关信息。包含如下属性。spring
property属性只容许指定Authentication所拥有的属性。apache
<!--获取当前用户的用户名-->
<@sec:authentication property="principal.username" />
复制代码
var属性用于指定一个属性名,这样当获取到了authentication的相关信息后会将其以var指定的属性名进行存放,默认是存放在pageConext中。能够经过scope属性进行指定。此外,当指定了var属性后,authentication标签不会将获取到的信息在页面上进行展现,如需展现用户应该经过var指定的属性进行展现,或去掉var属性。编程
<@sec.authentication property="principal.username" scope="session" var="username"/>
${username }
复制代码
该标签只有在与spring security
的acl模块一块儿使用时才有效。它会检查指定域对象的必需权限的逗号分隔列表。若是当前用户拥有全部这些权限,则会评估标签正文。若是他们不这样作,它将被跳过。小程序
<@sec.accesscontrollist hasPermission="1,2" domainObject="${someObject}">
若是用户具备给定对象上的值“1”或“2”表示的全部权限,则会显示此信息
</@sec.accesscontrollist>
复制代码
从个人 github 中下载,github.com/longfeizhen…微信小程序
🙂🙂🙂关注微信小程序java架构师历程 上下班的路上无聊吗?还在看小说、新闻吗?不知道怎样提升本身的技术吗?来吧这里有你须要的java架构文章,1.5w+的java工程师都在看,你还在等什么?tomcat