国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
基于注釋的Spring Security實戰(zhàn)指南

《基于注釋的spring Security實戰(zhàn)指南》

版權(quán)聲明:本文屬于原創(chuàng),版權(quán)歸作者chszs所有,使用源碼無任何限制,但轉(zhuǎn)載文章需經(jīng)作者同意。

一、準(zhǔn)備工作

預(yù)準(zhǔn)備的工具及軟件有:

1. Eclipse IDE:我使用Eclipse JEE 3.7版,即eclipse-jee-indigo-SR2-win32-x86_64.zip

2. JDK 7我使用JDK 7u4版,即jdk-7u4-windows-x64.exe

3. Spring Framework我使用Spring Framework 3.1.2版,即spring-framework-3.1.2.RELEASE-with-docs.zip

4. Spring Security:我使用Spring Security 3.1.2版,即spring-security-3.1.2.RELEASE-dist

5. 其它JAR包:jstl-1.2.jar,commons-logging-1.1.1.jar,cglib-nodep-2.2.jar

6. Tomcat應(yīng)用服務(wù)器:我使用Tomcat 7.0.29版,即apache-tomcat-7.0.29-windows-x64.zip

說明:

1. Eclipse IDEJDK 7的版本可以更高一些,不影響開發(fā)和調(diào)試。

2. Eclipse一定要下載JEE版。

3. Eclipse、JDKTomcat的安裝過程省略。

4. 我的操作系統(tǒng)64位版本,故開發(fā)環(huán)境對應(yīng)的工具都是下載64位的安裝包。

二、新建項目

Eclipse環(huán)境下新建Dynamic Web Project。

項目名為:SpringSecurityDemo,

Target runtime選擇New Runtime,然后選擇Apache Tomcat v7.0,并設(shè)置好Tomcat的安裝目錄。

連續(xù)點擊兩次Next,在“Generate web.xml deployment descriptor”處打勾選擇,并點擊Finish。


三、添加庫文件

把下列JAR文件添加到項目的WebContent\WEB-INF\lib目錄下。

四、業(yè)務(wù)層開發(fā)

1. 在項目src處,新建com.ch.configuration包,并新建WebConfig.Java類,內(nèi)容如下:

  1. package com.ch.configuration;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.ComponentScan;  
  5. import org.springframework.context.annotation.Configuration;  
  6. import org.springframework.context.annotation.ImportResource;  
  7. import org.springframework.web.servlet.ViewResolver;  
  8. import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  10. import org.springframework.web.servlet.view.InternalResourceViewResolver;  
  11.   
  12. @EnableWebMvc  
  13. @Configuration  
  14. @ComponentScan(basePackages = "com.jverstry")  
  15. @ImportResource("/WEB-INF/MyServlet-security.xml")  
  16. public class WebConfig extends WebMvcConfigurerAdapter {  
  17.   
  18.     @Bean  
  19.     public ViewResolver getViewResolver() {  
  20.         InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
  21.         resolver.setPrefix("WEB-INF/pages/");  
  22.         resolver.setSuffix(".jsp");  
  23.   
  24.         return resolver;  
  25.     }  
  26.   
  27. }  

2. 新建com.ch.configuration.controller包,并新建MyController.java類,內(nèi)容如下:

  1. package com.ch.configuration.controller;  
  2.   
  3. import com.ch.configuration.service.MyService;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9.   
  10. @Controller  
  11. public class MyController {  
  12.   
  13.     private MyService myService;  
  14.   
  15.     @Autowired  
  16.     public void setMyService(MyService myService) {  
  17.         this.myService = myService;  
  18.     }  
  19.   
  20.     @RequestMapping(value = "/")  
  21.     public String home() {  
  22.         return "index";  
  23.     }  
  24.   
  25.     @RequestMapping(value = "/getTime")  
  26.     public String helloWorld(Model model) {  
  27.         model.addAttribute("TimeIs", myService.getCurrentTimeInMilliseconds());  
  28.         return "getTime";  
  29.     }  
  30.   
  31. }  

3. 新建com.ch.configuration.service包,并新建MyService.java接口類,內(nèi)容如下:

  1. package com.ch.configuration.service;  
  2.   
  3. public interface MyService {  
  4.     long getCurrentTimeInMilliseconds();  
  5. }  

4. com.ch.configuration.service包新建MyServiceImpl.java類,內(nèi)容如下:

  1. package com.ch.configuration.service;  
  2.   
  3. public class MyServiceImpl implements MyService {  
  4.   
  5.     @Override  
  6.     public long getCurrentTimeInMilliseconds() {  
  7.         return System.currentTimeMillis();  
  8.     }  
  9.   
  10. }  

5. com.ch.configuration.service包新建MyServicesConfiguration.java類,內(nèi)容如下:

  1. package com.ch.configuration.service;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5.   
  6. @Configuration  
  7. public class MyServicesConfiguration {  
  8.   
  9.     private MyService myService = new MyServiceImpl();  
  10.   
  11.     @Bean  
  12.     public MyService getMyService() {  
  13.         return myService;  
  14.     }  
  15.   
  16. }  

五、前臺頁面層開發(fā)

1. WebContent\WEB-INF目錄新建pages文件夾,接著在pages目錄下新建getTime.jsp文件,內(nèi)容如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Get Time !!!</title>  
  9. </head>  
  10. <body>  
  11.     The time in milliseconds is:  
  12.     <c:out value="${TimeIs}" />  
  13.     !  
  14. </body>  
  15. </html>  

2. pages目錄下新建index.jsp文件,內(nèi)容如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Welcome !!!</title>  
  8. </head>  
  9. <body>  
  10.     <h1>Welcome To Spring MVC With Annotations !!!</h1>  
  11.     <h1>(with login...)</h1>  
  12. </body>  
  13. </html>  

3. 修改WEB-INF下的web.xml文件,內(nèi)容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <display-name>SpringSecurityDemo</display-name>  
  7.     <context-param>  
  8.         <param-name>contextClass</param-name>  
  9.         <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>  
  10.     </context-param>  
  11.   
  12.     <context-param>  
  13.         <param-name>contextConfigLocation</param-name>  
  14.         <param-value>com.ch.configuration</param-value>  
  15.     </context-param>  
  16.   
  17.     <filter>  
  18.         <filter-name>springSecurityFilterChain</filter-name>  
  19.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  20.     </filter>  
  21.   
  22.     <filter-mapping>  
  23.         <filter-name>springSecurityFilterChain</filter-name>  
  24.         <url-pattern>/*</url-pattern>  
  25.     </filter-mapping>  
  26.   
  27.     <listener>  
  28.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  29.     </listener>  
  30.   
  31.     <servlet>  
  32.         <servlet-name>MyServlet</servlet-name>  
  33.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  34.         <init-param>  
  35.             <param-name>contextConfigLocation</param-name>  
  36.             <param-value></param-value>  
  37.         </init-param>  
  38.         <load-on-startup>1</load-on-startup>  
  39.     </servlet>  
  40.   
  41.     <servlet-mapping>  
  42.         <servlet-name>MyServlet</servlet-name>  
  43.         <url-pattern>/</url-pattern>  
  44.     </servlet-mapping>  
  45.   
  46.     <welcome-file-list>  
  47.         <welcome-file></welcome-file>  
  48.     </welcome-file-list>  
  49. </web-app>  

4. WEB-INF下新建MyServlet-security.xml文件,內(nèi)容如下:

  1. <beans:beans xmlns="http://www.springframework.org/schema/security"  
  2.     xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5.     http://www.springframework.org/schema/security  
  6.     http://www.springframework.org/schema/security/spring-security-3.1.xsd">  
  7.   
  8.     <http auto-config="true">  
  9.         <intercept-url pattern="/*" access="ROLE_USER" />  
  10.     </http>  
  11.   
  12.     <authentication-manager alias="authenticationManager">  
  13.         <authentication-provider>  
  14.             <user-service>  
  15.                 <user authorities="ROLE_USER" name="guest" password="guest" />  
  16.             </user-service>  
  17.         </authentication-provider>  
  18.     </authentication-manager>  
  19.   
  20. </beans:beans>  

至此,Demo項目的開發(fā)已經(jīng)完成。項目的整體結(jié)構(gòu)圖如圖所示:



六、部署和運行

1. Eclipse選擇項目SpringSecurityDemo,右鍵選擇“Run As”,再選擇“Run on Server”,選擇Apache Tomcat v7.0,Eclipse IDE自動完成部署并運行。

在瀏覽器上輸入地址:http://localhost:8080/SpringSecurityDemo/

顯示如下:

注:地址自動被重定向到http://localhost:8080/SpringSecurityDemo/spring_security_login

User/Password輸入guest/guest,顯示:

如果輸入錯誤,顯示:

OK!本文就到這里,對于Spring的注釋,可以參考官方文檔加以理解。













本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
spring security 3.1配置過程從簡單到復(fù)雜詳細(xì)配置
淘淘商城8.6
Tomcat Axis Eclipse實例講解
用spirng和hessian構(gòu)建分布式應(yīng)用(遠(yuǎn)程接口)的方法[原] - Spring -...
使用Eclipse構(gòu)建Maven的SpringMVC項目
java – Spring中的事務(wù)錯誤
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服