这篇主要的内容
Spring Security 保护业务代码的执行
准备工作 .
1.创建HelloService接口
package zyk.service; //import org.springframework.security.access.annotation.Secured; public interface HelloService { //@Secured({ "ROLE_USER", "ROLE_ADMIN" }) public String sayHi(String userName); //@Secured({"ROLE_ADMIN"}) public String sayBye(String userName); }
2.实现类HelloServiceImpl
package zyk.service.impl; import zyk.service.HelloService; public class HelloServiceImpl implements HelloService { public String sayHi(String userName) { return "大家好!我是:" + userName; } public String sayBye(String userName) { return userName + " 跟大家说再见!"; } }
3.配置applicationContext.xml 使HelloService 交给Spring 管理.
<bean id="helloService" class="zyk.service.impl.HelloServiceImpl" />
4.创建 HelloServlet
package zyk.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import zyk.service.HelloService; public class HelloServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ public HelloServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String userName = request.getParameter("userName"); String method = request.getParameter("method"); ApplicationContext ctx = WebApplicationContextUtils .getWebApplicationContext(this.getServletContext()); HelloService helloService = ctx.getBean("helloService", HelloService.class); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE></TITLE></HEAD>"); out.println(" <BODY>"); if (method.equals("sayHi")) { out.println(helloService.sayHi(userName)); } else { out.println(helloService.sayBye(userName)); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
5.在web.xml 中配置 HelloServlet 的映射路径.
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>HelloServlet</servlet-name> <servlet-class>zyk.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.action</url-pattern> </servlet-mapping>
6.在Index.jsp 中添加链接.
<a href="${pageContext.request.contextPath}/hello.action?method=sayHi&userName=<sec:authentication property="name" />">SayHi!</a> <br /> <a href="${pageContext.request.contextPath}/hello.action?method=sayBye&userName=<sec:authentication property="name" />">SayBye!</a>
第一次测试 User 和 admin 均可以调用 SayHi 和 SayBye 方法.
接下来 要实现的是
admin 可以 调用 SayHi 和 SayBye 方法.
user 只能 调用 SayHi 方法..
A)使用XML的方式
1.在applicationContext.xml 中 配置
<!-- XML 的方式 --> <security:global-method-security> <!-- 拥有ROLE_USER或者ROLE_ADMIN 权限的用户 可以访问 包 zyk.service 下的任意个类 里 返回值类型为任意类型 并 方法名为sayHi 的方法--> <security:protect-pointcut access="ROLE_USER,ROLE_ADMIN" expression="execution(* zyk.service.*.sayHi(..))"/> <!-- 第一个* :表示返回任意类型 第二个 * :表示任意的类 第三个* : 以say开头的任意方法 名 对应的是 : 拥有ROLE_ADMIN 权限的用户 可以访问 包 zyk.service 下的任意个类 里 返回值类型为任意类型 并以say开头的方法 (例如 sayHi 和 sayBye) --> <security:protect-pointcut access="ROLE_ADMIN" expression="execution(* zyk.service.*.say*(..))"/> </security:global-method-security>
第二次测试 Ok 。将上面的配置注释掉.换用Annotation 的方式 .
B)使用Annotation的方式
1.启用Annotation 配置applicationContext.xml
<!-- 启用annotation --> <security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled" />
2.给HelloService接口里的方法加上 SpringSecurity的注解.用法很明显.
package zyk.service; import org.springframework.security.access.annotation.Secured; public interface HelloService { @Secured({ "ROLE_USER", "ROLE_ADMIN" }) public String sayHi(String userName); @Secured({"ROLE_ADMIN"}) public String sayBye(String userName); }
再次测试 Ok。
到此学习的资料 全部来自第一篇下的附件.《一步一步教你使用SpringSecurity》
相关推荐
三更springsecurity学习笔记
SpringSecurity学习总结源代码
Spring Security学习总结一
spring security方面的学习资料,包含:Spring+Security+3+与+CAS单点登录配置;Spring+Security3中文教程;Spring-Security安全权限管理手册;Spring+Security文库;还有一个学习笔记!
Spring Security学习总结二
Spring Security OAuth2.0学习笔记 什么是认证、授权、会话。 Java Servlet为支持http会话做了哪些事儿。 基于session认证机制的运作流程。 基于token认证机制的运作流程。 理解Spring Security的工作原理,Spring ...
Shiro+Spring Security学习文档,Spring+Security-3.0.1中文官方文档.pdf,跟我学Shiro教程.pdf。。。
spring security学习总结文档是我自己在学习security之后总结的,该部分是初级部分
SpringSecurity视频讲解,内含课程演示的源代码,,,,,,
Spring Security是一个功能强大且高度可定制的身份验证和授权框架,专门用于保护Java应用程序的安全性。它构建在Spring Framework基础之上,提供了全面的安全解决方案,包括身份验证、授权、攻击防护等功能。 Spring...
SpringSecurity学习总结一.pdf
适合Java新手的学习资源:介绍针对初学者设计的Spring Security学习资源,例如教程、视频课程或者书籍,并强调这些资源将如何帮助他们轻松入门并掌握关键概念。 学习收益:描述学习Spring Security的收益,例如提升...
family168上的学习spring security的资料,chm形式
和spring集成使用的完美权限框架,学习java一定要学会spring_security
视频详细讲解,需要的小伙伴自行网盘下载,链接见附件,永久有效。 首先,SSM环境中我们通过xml配置的...Springsecurity在两种不同的开发模式中使用,有经典的独立web后台管理系统,也有时下最流行的前后端分离场景。
SpringSecurity中文文档 2个 学习SpringSecurity必备
Spring Security-3中文官方文档(及教程),另有一篇详细的教程文档。
Spring Security学习总结 关于Spring Security的完全学习总结 强烈推荐
即使用一个基于Spring3的web工程作为基础,以理解使用Spring Security3使其保证安全的概念和策略。 不管你是不是已经使用Spring Security还是只是对这个软件有兴趣,就都会在本书中得到有用的信息。 在本节的内容中,你...