spring security 在jsp中的标签库

spring-security 在jsp中的标签库
1.在jsp中声明
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

2.标签
目前共有三个标签
   
<sec:authorize></sec:authorize>      
<sec:authentication property=""></sec:authentication>
<sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>

      
2.1、authorize标签
这个标签用来决定它的内容是否会被执行.
<sec:authorize access="hasRole('supervisor')">
    This content will only be visible to users who have
    the "supervisor" authority in their list of GrantedAuthoritys.
</sec:authorize>



显示一个特定的链接,如果用户允许点击它.
<sec:authorize url="/admin">
    This content will only be visible to users who are authorized to send requests to the "/admin" URL.
</sec:authorize>



2.2、authentication标签
这个标签允许访问当前的Authentication 对象, 保存在安全上下文中。
比如,如果Authentication 的principal 属性是Spring Security 的UserDetails 对象的一个实例,
就要使用
<sec:authentication property="principal.username" /> 

来渲染当前用户的名称。

当然,它不必使用JSP 标签来实现这些功能,一些人更愿意在视图中保持逻辑越少越好。你可以在你的MVC 控制器中访问Authentication 对象( 通过调用
SecurityContextHolder.getContext().getAuthentication()) 然后直接在模型中添加数据,来渲染视图。

2.3、accesscontrollist标签
这个标签纸在使用Spring Security ACL 模块时才可以使用。它检测一个用逗号分隔的特
定领域对象的需要权限列表。如果当前用户拥有这些权限的任何一个,标签内容就会被执行。
否则,就会被略过。
<sec:accesscontrollist hasPermission="1,2" domainObject="${someObject}">
    This will be shown if the user has either of the permissions
    represented by the values "1" or "2" on the given object.
</sec:accesscontrollist>

参考: Spring_Security-3.0.1_中文官方文档(翻译版).pdf

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SSM(Spring+Spring MVC+MyBatis)项目使用Spring Security可以提供身份验证和授权功能,以保护应用程序的安全性。下面是在SSM项目使用Spring Security的一般步骤: 1. 添加依赖:在项目的pom.xml文件添加Spring Security的依赖。例如: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.4.6</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.4.6</version> </dependency> ``` 2. 配置Spring Security:在Spring的配置文件(通常是applicationContext.xml)配置Spring Security。可以使用XML配置或Java配置。 - XML配置示例: ```xml <import resource="classpath:security-config.xml" /> ``` - Java配置示例: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 配置身份验证和授权规则 @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasAnyRole("USER", "ADMIN") .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/login?logout") .permitAll(); } // 配置用户认证的数据源 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("ADMIN"); } } ``` 3. 创建登录和注销页面:创建登录页面(例如login.jsp)和注销页面(例如logout.jsp)。 4. 对需要保护的资源进行配置:根据应用程序的需求,对需要保护的URL进行配置,以限制访问只有经过身份验证和授权的用户才能访问。 以上是在SSM项目使用Spring Security的基本步骤,具体的配置和使用方式可以根据项目的需求进行调整。另外,还可以使用数据等其他方式来进行用户认证和授权。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值