日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

基于注释的Spring Security实战

發(fā)布時間:2025/3/20 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于注释的Spring Security实战 小編覺得挺不錯的,現(xià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應用服務器:我使用Tomcat?7.0.29版,即apache-tomcat-7.0.29-windows-x64.zip

?

說明:

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

2.?Eclipse一定要下載JEE版。

3.?Eclipse、JDK和Tomcat的安裝過程省略。

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

?

二、新建項目

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

項目名為:SpringSecurityDemo,

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

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

?

三、添加庫文件

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

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

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


  • package?com.ch.configuration;??
  • ??
  • import?org.springframework.context.annotation.Bean;??
  • import?org.springframework.context.annotation.ComponentScan;??
  • import?org.springframework.context.annotation.Configuration;??
  • import?org.springframework.context.annotation.ImportResource;??
  • import?org.springframework.web.servlet.ViewResolver;??
  • import?org.springframework.web.servlet.config.annotation.EnableWebMvc;??
  • import?org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;??
  • import?org.springframework.web.servlet.view.InternalResourceViewResolver;??
  • ??
  • @EnableWebMvc??
  • @Configuration??
  • @ComponentScan(basePackages?=?"com.jverstry")??
  • @ImportResource("/WEB-INF/MyServlet-security.xml")??
  • public?class?WebConfig?extends?WebMvcConfigurerAdapter?{??
  • ??
  • ????@Bean??
  • ????public?ViewResolver?getViewResolver()?{??
  • ????????InternalResourceViewResolver?resolver?=?new?InternalResourceViewResolver();??
  • ????????resolver.setPrefix("WEB-INF/pages/");??
  • ????????resolver.setSuffix(".jsp");??
  • ??
  • ????????return?resolver;??
  • ????}??
  • ??
  • }??

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

    ?


  • package?com.ch.configuration.controller;??
  • ??
  • import?com.ch.configuration.service.MyService;??
  • ??
  • import?org.springframework.beans.factory.annotation.Autowired;??
  • import?org.springframework.stereotype.Controller;??
  • import?org.springframework.ui.Model;??
  • import?org.springframework.web.bind.annotation.RequestMapping;??
  • ??
  • @Controller??
  • public?class?MyController?{??
  • ??
  • ????private?MyService?myService;??
  • ??
  • ????@Autowired??
  • ????public?void?setMyService(MyService?myService)?{??
  • ????????this.myService?=?myService;??
  • ????}??
  • ??
  • ????@RequestMapping(value?=?"/")??
  • ????public?String?home()?{??
  • ????????return?"index";??
  • ????}??
  • ??
  • ????@RequestMapping(value?=?"/getTime")??
  • ????public?String?helloWorld(Model?model)?{??
  • ????????model.addAttribute("TimeIs",?myService.getCurrentTimeInMilliseconds());??
  • ????????return?"getTime";??
  • ????}??
  • ??
  • }??

  • ?

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


  • package?com.ch.configuration.service;??
  • ??
  • public?interface?MyService?{??
  • ????long?getCurrentTimeInMilliseconds();??
  • }??
  • 4.?在com.ch.configuration.service包新建MyServiceImpl.java類,內(nèi)容如下:


  • package?com.ch.configuration.service;??
  • ??
  • public?class?MyServiceImpl?implements?MyService?{??
  • ??
  • ????@Override??
  • ????public?long?getCurrentTimeInMilliseconds()?{??
  • ????????return?System.currentTimeMillis();??
  • ????}??
  • ??
  • }??
  • 5.?在com.ch.configuration.service包新建MyServicesConfiguration.java類,內(nèi)容如下:

    package com.ch.configuration.service; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyServicesConfiguration { private MyService myService = new MyServiceImpl(); @Bean public MyService getMyService() { return myService; } }

    ?


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

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


  • <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
  • ????pageEncoding="UTF-8"%>??
  • <%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>??
  • >??
  • <html>??
  • <head>??
  • <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
  • <title>Get?Time?!!!title>??
  • head>??
  • <body>??
  • ????The?time?in?milliseconds?is:??
  • ????<c:out?value="${TimeIs}"?/>??
  • ????!??
  • body>??
  • html>??
  • 2.?在pages目錄下新建index.jsp文件,內(nèi)容如下:


  • <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
  • ????pageEncoding="UTF-8"%>??
  • >??
  • <html>??
  • <head>??
  • <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
  • <title>Welcome?!!!title>??
  • head>??
  • <body>??
  • ????<h1>Welcome?To?Spring?MVC?With?Annotations?!!!h1>??
  • ????<h1>(with?login...)h1>??
  • body>??
  • html>??
  • 3.?修改WEB-INF下的web.xml文件,內(nèi)容如下:


  • xml?version="1.0"?encoding="UTF-8"?>??
  • <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  • ????xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
  • ????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"??
  • ????id="WebApp_ID"?version="3.0">??
  • ????<display-name>SpringSecurityDemodisplay-name>??
  • ????<context-param>??
  • ????????<param-name>contextClassparam-name>??
  • ????????<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>??
  • ????context-param>??
  • ??
  • ????<context-param>??
  • ????????<param-name>contextConfigLocationparam-name>??
  • ????????<param-value>com.ch.configurationparam-value>??
  • ????context-param>??
  • ??
  • ????<filter>??
  • ????????<filter-name>springSecurityFilterChainfilter-name>??
  • ????????<filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>??
  • ????filter>??
  • ??
  • ????<filter-mapping>??
  • ????????<filter-name>springSecurityFilterChainfilter-name>??
  • ????????<url-pattern>/*url-pattern>??
  • ????filter-mapping>??
  • ??
  • ????<listener>??
  • ????????<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>??
  • ????listener>??
  • ??
  • ????<servlet>??
  • ????????<servlet-name>MyServletservlet-name>??
  • ????????<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>??
  • ????????<init-param>??
  • ????????????<param-name>contextConfigLocationparam-name>??
  • ????????????<param-value>param-value>??
  • ????????init-param>??
  • ????????<load-on-startup>1load-on-startup>??
  • ????servlet>??
  • ??
  • ????<servlet-mapping>??
  • ????????<servlet-name>MyServletservlet-name>??
  • ????????<url-pattern>/url-pattern>??
  • ????servlet-mapping>??
  • ??
  • ????<welcome-file-list>??
  • ????????<welcome-file>welcome-file>??
  • ????welcome-file-list>??
  • web-app>??
  • 4.?在WEB-INF下新建MyServlet-security.xml文件,內(nèi)容如下:


  • <beans:beans?xmlns="http://www.springframework.org/schema/security"??
  • ????xmlns:beans="http://www.springframework.org/schema/beans"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  • ????xsi:schemaLocation="http://www.springframework.org/schema/beans??
  • ????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  • ????http://www.springframework.org/schema/security??
  • ????http://www.springframework.org/schema/security/spring-security-3.1.xsd">??
  • ??
  • ????<http?auto-config="true">??
  • ????????<intercept-url?pattern="/*"?access="ROLE_USER"?/>??
  • ????http>??
  • ??
  • ????<authentication-manager?alias="authenticationManager">??
  • ????????<authentication-provider>??
  • ????????????<user-service>??
  • ????????????????<user?authorities="ROLE_USER"?name="guest"?password="guest"?/>??
  • ????????????user-service>??
  • ????????authentication-provider>??
  • ????authentication-manager>??
  • ??
  • beans:beans>??

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



    ?

    六、部署和運行

    ?

    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的注釋,可以參考官方文檔加以理解。

    轉(zhuǎn)載于:https://www.cnblogs.com/jokerjason/p/6000176.html

    總結(jié)

    以上是生活随笔為你收集整理的基于注释的Spring Security实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。