Spring Security3实现,权限动态获取

采用Maven管理,Spring Mvc Jpa等技术

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.wiker</groupId>
  5. <artifactId>springSecurity</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>springSecurity Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10.  
  11. <properties>
  12. <!-- 主要依赖库的版本定义 -->
  13. <c3p0.version>0.9.1.2</c3p0.version>
  14. <spring-hibernate3.version>2.0.8</spring-hibernate3.version>
  15. <javax.transaction.version>1.1</javax.transaction.version>
  16. <org.kohsuke.stapler.version>2.1-rev6</org.kohsuke.stapler.version>
  17. <spring-web.version>3.2.3.RELEASE</spring-web.version>
  18. <commons-lang.version>2.5</commons-lang.version>
  19. <javax.xml.rpc.version>1.1</javax.xml.rpc.version>
  20. <commons-email.version>1.1</commons-email.version>
  21. <org.apache.axis.version>1.4</org.apache.axis.version>
  22. <commons-fileupload.version>1.3</commons-fileupload.version>
  23. <jdom.version>1.1</jdom.version>
  24. <javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
  25. <javax.servlet.api.version>2.5</javax.servlet.api.version>
  26. <jxl.version>2.6.12</jxl.version>
  27. <javax.activation.version>1.1.1</javax.activation.version>
  28. <javax.mail.version>1.4.5</javax.mail.version>
  29. <spring.version>3.2.3.RELEASE</spring.version>
  30. <hibernate.version>4.1.8.Final</hibernate.version>
  31. <hibernate.core.version>4.1.8.Final</hibernate.core.version>
  32. <spring-data-jpa.version>1.2.0.RELEASE</spring-data-jpa.version>
  33. <commons-dbcp.version>1.4</commons-dbcp.version>
  34. <sitemesh.version>2.4.2</sitemesh.version>
  35. <hibernate-validator.version>4.3.0.Final</hibernate-validator.version>
  36. <jackson.version>2.1.2</jackson.version>
  37. <jackson-asl.version>1.9.5</jackson-asl.version>
  38. <slf4j.version>1.7.2</slf4j.version>
  39. <log4j.version>1.2.17</log4j.version>
  40. <commons-lang3.version>3.1</commons-lang3.version>
  41. <guava.version>13.0.1</guava.version>
  42. <quartz.version>1.5.2</quartz.version>
  43. <freemarker.version>2.3.19</freemarker.version>
  44. <httpclient.version>4.2.2</httpclient.version>
  45. <commons-httpclient.version>3.0.1</commons-httpclient.version>
  46. <joda-time.version>2.1</joda-time.version>
  47. <junit.version>4.11</junit.version>
  48. <testng.version>6.3</testng.version>
  49. <mockito.version>1.9.5</mockito.version>
  50. <selenium.version>2.28.0</selenium.version>
  51. <jetty.version>7.6.8.v20121106</jetty.version>
  52. <h2.version>1.3.170</h2.version>
  53. <codec.version>1.6</codec.version>
  54. <dom4j.version>1.6.1</dom4j.version>
  55. <lombok.version>0.11.0</lombok.version>
  56. <wro4j.version>1.4.7</wro4j.version>
  57. <wro4j.extensions.version>1.4.7</wro4j.extensions.version>
  58. <commons-io.version>2.4</commons-io.version>
  59. <commons-collections.version>3.2.1</commons-collections.version>
  60. <gson.version>2.2.2</gson.version>
  61. <pinyin4j.version>2.5.0</pinyin4j.version>
  62. <wicked-charts.version>1.4.3</wicked-charts.version>
  63. <batik-all.version>1.8pre-r1084380</batik-all.version>
  64. <rhino.version>1.7R4</rhino.version>
  65. <thumbnailator.version>[0.4, 0.5)</thumbnailator.version>
  66. <struts2.core.version>2.3.14.3</struts2.core.version>
  67. <wsdl4j.version>1.5.1</wsdl4j.version>
  68. <aspectjrt.version>1.7.1</aspectjrt.version>
  69. <velocity.version>1.7</velocity.version>
  70. <antlr.version>2.7.6</antlr.version>
  71. <jchardet.version>1.0</jchardet.version>
  72. <google-collection.version>1.0</google-collection.version>
  73. <json-lib.version>2.4</json-lib.version>
  74. <urlrewritefilter.version>4.0.3</urlrewritefilter.version>
  75.  
  76. <!-- Plugin的属性定义 -->
  77. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  78. <jdk.version>1.6</jdk.version>
  79.  
  80. <server.url>http://localhost:8080/manager/html</server.url>
  81. <server.user>admin</server.user>
  82. <server.password>admin</server.password>
  83.  
  84. <jdbc.driver.groupId>mysql</jdbc.driver.groupId>
  85. <jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId>
  86. <jdbc.driver.version>5.1.21</jdbc.driver.version>
  87. </properties>
  88. <dependencies>
  89. <dependency>
  90. <groupId>com.google.collections</groupId>
  91. <artifactId>google-collections</artifactId>
  92. <version>${google-collection.version}</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.springframework</groupId>
  96. <artifactId>spring-core</artifactId>
  97. <version>${spring.version}</version>
  98. </dependency>
  99. <!-- <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId>
  100. <version>${org.apache.axis2.version}</version> </dependency> -->
  101. <dependency>
  102. <groupId>org.apache.axis</groupId>
  103. <artifactId>axis</artifactId>
  104. <version>${org.apache.axis.version}</version>
  105. </dependency>
  106. <dependency>
  107. <groupId>org.springframework</groupId>
  108. <artifactId>spring-beans</artifactId>
  109. <version>${spring.version}</version>
  110. </dependency>
  111.  
  112. <dependency>
  113. <groupId>commons-lang</groupId>
  114. <artifactId>commons-lang</artifactId>
  115. <version>${commons-lang.version}</version>
  116. </dependency>
  117. <dependency>
  118. <groupId>org.springframework</groupId>
  119. <artifactId>spring-context</artifactId>
  120. <version>${spring.version}</version>
  121. </dependency>
  122. <dependency>
  123. <groupId>org.springframework</groupId>
  124. <artifactId>spring-aop</artifactId>
  125. <version>${spring.version}</version>
  126. </dependency>
  127. <dependency>
  128. <groupId>org.springframework</groupId>
  129. <artifactId>spring-context-support</artifactId>
  130. <version>${spring.version}</version>
  131. </dependency>
  132.  
  133. <!-- Spring Security -->
  134. <dependency>
  135. <groupId>org.springframework.security</groupId>
  136. <artifactId>spring-security-core</artifactId>
  137. <version>3.1.4.RELEASE</version>
  138. </dependency>
  139. <dependency>
  140. <groupId>mysql</groupId>
  141. <artifactId>mysql-connector-java</artifactId>
  142. <version>5.1.18</version>
  143. </dependency>
  144. <dependency>
  145. <groupId>org.springframework.security</groupId>
  146. <artifactId>spring-security-web</artifactId>
  147. <version>3.1.4.RELEASE</version>
  148. </dependency>
  149. <dependency>
  150. <groupId>org.springframework.security</groupId>
  151. <artifactId>spring-security-config</artifactId>
  152. <version>3.1.4.RELEASE</version>
  153. </dependency>
  154.  
  155. <dependency>
  156. <groupId>org.aspectj</groupId>
  157. <artifactId>aspectjrt</artifactId>
  158. <version>${aspectjrt.version}</version>
  159. </dependency>
  160. <dependency>
  161. <groupId>wsdl4j</groupId>
  162. <artifactId>wsdl4j-qname</artifactId>
  163. <version>${wsdl4j.version}</version>
  164. </dependency>
  165. <dependency>
  166. <groupId>org.aspectj</groupId>
  167. <artifactId>aspectjweaver</artifactId>
  168. <version>${aspectjrt.version}</version>
  169. </dependency>
  170. <dependency>
  171. <groupId>cglib</groupId>
  172. <artifactId>cglib</artifactId>
  173. <version>2.2.2</version>
  174. <scope>runtime</scope>
  175. </dependency>
  176. <dependency>
  177. <groupId>asm</groupId>
  178. <artifactId>asm</artifactId>
  179. <version>3.3.1</version>
  180. <scope>runtime</scope>
  181. </dependency>
  182.  
  183. <dependency>
  184. <groupId>org.hibernate</groupId>
  185. <artifactId>hibernate-entitymanager</artifactId>
  186. <version>${hibernate.version}</version>
  187. </dependency>
  188. <dependency>
  189. <groupId>org.hibernate</groupId>
  190. <artifactId>hibernate-core</artifactId>
  191. <version>${hibernate.core.version}</version>
  192. </dependency>
  193. <dependency>
  194. <groupId>javax.xml.rpc</groupId>
  195. <artifactId>javax.xml.rpc-api</artifactId>
  196. <version>${javax.xml.rpc.version}</version>
  197. </dependency>
  198. <!-- spring data access -->
  199. <dependency>
  200. <groupId>org.springframework.data</groupId>
  201. <artifactId>spring-data-jpa</artifactId>
  202. <version>${spring-data-jpa.version}</version>
  203. <exclusions>
  204. <exclusion>
  205. <groupId>junit</groupId>
  206. <artifactId>junit-dep</artifactId>
  207. </exclusion>
  208. </exclusions>
  209. </dependency>
  210. <dependency>
  211. <groupId>org.springframework</groupId>
  212. <artifactId>spring-orm</artifactId>
  213. <version>${spring.version}</version>
  214. </dependency>
  215. <dependency>
  216. <groupId>net.sf.json-lib</groupId>
  217. <artifactId>json-lib</artifactId>
  218. <version>${json-lib.version}</version>
  219. <classifier>jdk15</classifier>
  220. </dependency>
  221. <dependency>
  222. <groupId>org.tuckey</groupId>
  223. <artifactId>urlrewritefilter</artifactId>
  224. <version>${urlrewritefilter.version}</version>
  225. </dependency>
  226. <dependency>
  227. <groupId>org.springframework</groupId>
  228. <artifactId>spring-jdbc</artifactId>
  229. <version>${spring.version}</version>
  230. </dependency>
  231. <dependency>
  232. <groupId>javax.annotation</groupId>
  233. <artifactId>javax.annotation-api</artifactId>
  234. <version>1.2</version>
  235. </dependency>
  236.  
  237. <!-- dbcp connection pool -->
  238. <dependency>
  239. <groupId>commons-dbcp</groupId>
  240. <artifactId>commons-dbcp</artifactId>
  241. <version>${commons-dbcp.version}</version>
  242. <scope>runtime</scope>
  243. </dependency>
  244.  
  245. <!-- PERSISTENCE end -->
  246. <!-- WEB begin -->
  247. <dependency>
  248. <groupId>org.springframework</groupId>
  249. <artifactId>spring-webmvc</artifactId>
  250. <version>${spring.version}</version>
  251. </dependency>
  252. <dependency>
  253. <groupId>jdom</groupId>
  254. <artifactId>jdom</artifactId>
  255. <version>${jdom.version}</version>
  256. </dependency>
  257. <dependency>
  258. <groupId>javax.servlet</groupId>
  259. <artifactId>jstl</artifactId>
  260. <version>${javax.servlet.jstl.version}</version>
  261. </dependency>
  262. <dependency>
  263. <groupId>net.sourceforge.jexcelapi</groupId>
  264. <artifactId>jxl</artifactId>
  265. <version>${jxl.version}</version>
  266. </dependency>
  267.  
  268. <dependency>
  269. <groupId>javax.servlet</groupId>
  270. <artifactId>servlet-api</artifactId>
  271. <version>${javax.servlet.api.version}</version>
  272. <scope>provided</scope>
  273. </dependency>
  274. <!-- JSR303 BeanValidator -->
  275. <dependency>
  276. <groupId>org.hibernate</groupId>
  277. <artifactId>hibernate-validator</artifactId>
  278. <version>${hibernate-validator.version}</version>
  279. </dependency>
  280. <!-- pinyin4j begin -->
  281. <!-- <dependency> <groupId>pinyin4j</groupId> <artifactId>pinyin4j</artifactId>
  282. <version>${pinyin4j.version}</version> </dependency> -->
  283. <!-- pinyin4j end -->
  284. <!-- JSON begin -->
  285. <dependency>
  286. <groupId>com.fasterxml.jackson.core</groupId>
  287. <artifactId>jackson-databind</artifactId>
  288. <version>${jackson.version}</version>
  289. </dependency>
  290. <dependency>
  291. <groupId>com.fasterxml.jackson.module</groupId>
  292. <artifactId>jackson-module-jaxb-annotations</artifactId>
  293. <version>${jackson.version}</version>
  294. </dependency>
  295. <dependency>
  296. <groupId>com.fasterxml.jackson.dataformat</groupId>
  297. <artifactId>jackson-dataformat-xml</artifactId>
  298. <version>${jackson.version}</version>
  299. </dependency>
  300. <dependency>
  301. <groupId>org.codehaus.jackson</groupId>
  302. <artifactId>jackson-core-asl</artifactId>
  303. <version>${jackson-asl.version}</version>
  304. </dependency>
  305. <dependency>
  306. <groupId>org.codehaus.jackson</groupId>
  307. <artifactId>jackson-mapper-asl</artifactId>
  308. <version>${jackson-asl.version}</version>
  309. </dependency>
  310. <dependency>
  311. <groupId>org.codehaus.jackson</groupId>
  312. <artifactId>jackson-jaxrs</artifactId>
  313. <version>${jackson-asl.version}</version>
  314. </dependency>
  315. <dependency>
  316. <groupId>org.codehaus.jackson</groupId>
  317. <artifactId>jackson-xc</artifactId>
  318. <version>${jackson-asl.version}</version>
  319. </dependency>
  320. <dependency>
  321. <groupId>com.google.code.gson</groupId>
  322. <artifactId>gson</artifactId>
  323. <version>${gson.version}</version>
  324. </dependency>
  325. <dependency>
  326. <groupId>quartz</groupId>
  327. <artifactId>quartz</artifactId>
  328. <version>${quartz.version}</version>
  329. </dependency>
  330. <!-- JSON end -->
  331.  
  332. <!-- LOGGING begin -->
  333. <dependency>
  334. <groupId>org.slf4j</groupId>
  335. <artifactId>slf4j-api</artifactId>
  336. <version>${slf4j.version}</version>
  337. </dependency>
  338. <dependency>
  339. <groupId>org.slf4j</groupId>
  340. <artifactId>slf4j-log4j12</artifactId>
  341. <version>${slf4j.version}</version>
  342. <scope>runtime</scope>
  343. </dependency>
  344. <!-- common-logging 实际调用slf4j -->
  345. <dependency>
  346. <groupId>org.slf4j</groupId>
  347. <artifactId>jcl-over-slf4j</artifactId>
  348. <version>${slf4j.version}</version>
  349. <scope>runtime</scope>
  350. </dependency>
  351. <!-- java.util.logging 实际调用slf4j -->
  352. <dependency>
  353. <groupId>org.slf4j</groupId>
  354. <artifactId>jul-to-slf4j</artifactId>
  355. <version>${slf4j.version}</version>
  356. <scope>runtime</scope>
  357. </dependency>
  358. <dependency>
  359. <groupId>log4j</groupId>
  360. <artifactId>log4j</artifactId>
  361. <version>${log4j.version}</version>
  362. </dependency>
  363. <dependency>
  364. <groupId>org.lazyluke</groupId>
  365. <artifactId>log4jdbc-remix</artifactId>
  366. <version>0.2.7</version>
  367. <scope>runtime</scope>
  368. </dependency>
  369. <!-- LOGGING end -->
  370. <dependency>
  371. <groupId>org.apache.commons</groupId>
  372. <artifactId>commons-lang3</artifactId>
  373. <version>${commons-lang3.version}</version>
  374. </dependency>
  375. <dependency>
  376. <groupId>org.kohsuke.stapler</groupId>
  377. <artifactId>json-lib</artifactId>
  378. <version>${org.kohsuke.stapler.version}</version>
  379. </dependency>
  380. <dependency>
  381. <groupId>org.springframework</groupId>
  382. <artifactId>spring-web</artifactId>
  383. <version>${spring-web.version}</version>
  384. </dependency>
  385. <dependency>
  386. <groupId>org.springframework</groupId>
  387. <artifactId>spring-hibernate3</artifactId>
  388. <version>${spring-hibernate3.version}</version>
  389. </dependency>
  390. <dependency>
  391. <groupId>com.google.guava</groupId>
  392. <artifactId>guava</artifactId>
  393. <version>${guava.version}</version>
  394. </dependency>
  395. <dependency>
  396. <groupId>commons-codec</groupId>
  397. <artifactId>commons-codec</artifactId>
  398. <version>${codec.version}</version>
  399. </dependency>
  400. <dependency>
  401. <groupId>commons-io</groupId>
  402. <artifactId>commons-io</artifactId>
  403. <version>${commons-io.version}</version>
  404. </dependency>
  405. <dependency>
  406. <groupId>commons-collections</groupId>
  407. <artifactId>commons-collections</artifactId>
  408. <version>${commons-collections.version}</version>
  409. </dependency>
  410. <dependency>
  411. <groupId>commons-fileupload</groupId>
  412. <artifactId>commons-fileupload</artifactId>
  413. <version>${commons-fileupload.version}</version>
  414. </dependency>
  415. <!-- GENERAL UTILS end -->
  416.  
  417. <!-- OTHER TOOLS begin -->
  418. <!-- httpclient -->
  419. <dependency>
  420. <groupId>org.apache.httpcomponents</groupId>
  421. <artifactId>httpclient</artifactId>
  422. <version>${httpclient.version}</version>
  423. <exclusions>
  424. <exclusion>
  425. <groupId>commons-logging</groupId>
  426. <artifactId>commons-logging</artifactId>
  427. </exclusion>
  428. </exclusions>
  429. </dependency>
  430. <dependency>
  431. <groupId>commons-httpclient</groupId>
  432. <artifactId>commons-httpclient</artifactId>
  433. <version>${commons-httpclient.version}</version>
  434. </dependency>
  435.  
  436. <!-- template engine -->
  437. <!-- <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId>
  438. <version>${freemarker.version}</version> </dependency> -->
  439.  
  440. <!-- third party dependencies -->
  441. <dependency>
  442. <groupId>org.projectlombok</groupId>
  443. <artifactId>lombok</artifactId>
  444. <version>${lombok.version}</version>
  445. <scope>provided</scope>
  446. </dependency>
  447. <dependency>
  448. <groupId>dom4j</groupId>
  449. <artifactId>dom4j</artifactId>
  450. <version>${dom4j.version}</version>
  451. </dependency>
  452.  
  453. <!-- email -->
  454. <dependency>
  455. <groupId>javax.mail</groupId>
  456. <artifactId>mail</artifactId>
  457. <version>${javax.mail.version}</version>
  458. </dependency>
  459. <dependency>
  460. <groupId>commons-email</groupId>
  461. <artifactId>commons-email</artifactId>
  462. <version>${commons-email.version}</version>
  463. </dependency>
  464. <dependency>
  465. <groupId>javax.activation</groupId>
  466. <artifactId>activation</artifactId>
  467. <version>${javax.activation.version}</version>
  468. </dependency>
  469. <dependency>
  470. <groupId>javax.transaction</groupId>
  471. <artifactId>jta</artifactId>
  472. <version>${javax.transaction.version}</version>
  473. </dependency>
  474. <dependency>
  475. <groupId>org.apache.velocity</groupId>
  476. <artifactId>velocity</artifactId>
  477. <version>${velocity.version}</version>
  478. </dependency>
  479. <dependency>
  480. <groupId>c3p0</groupId>
  481. <artifactId>c3p0</artifactId>
  482. <version>${c3p0.version}</version>
  483. </dependency>
  484.  
  485. <dependency>
  486. <groupId>junit</groupId>
  487. <artifactId>junit</artifactId>
  488. <version>${junit.version}</version>
  489. <scope>test</scope>
  490. </dependency>
  491.  
  492. <dependency>
  493. <groupId>org.mockito</groupId>
  494. <artifactId>mockito-core</artifactId>
  495. <version>${mockito.version}</version>
  496. <scope>test</scope>
  497. </dependency>
  498. <dependency>
  499. <groupId>org.mockito</groupId>
  500. <artifactId>mockito-all</artifactId>
  501. <version>${mockito.version}</version>
  502. <scope>test</scope>
  503. </dependency>
  504.  
  505. <dependency>
  506. <groupId>org.springframework</groupId>
  507. <artifactId>spring-test</artifactId>
  508. <version>${spring.version}</version>
  509. <scope>test</scope>
  510. </dependency>
  511. <!-- h2 -->
  512. <dependency>
  513. <groupId>com.h2database</groupId>
  514. <artifactId>h2</artifactId>
  515. <version>${h2.version}</version>
  516. <scope>test</scope>
  517. </dependency>
  518.  
  519. <dependency>
  520. <groupId>antlr</groupId>
  521. <artifactId>antlr</artifactId>
  522. <version>${antlr.version}</version>
  523. </dependency>
  524. <dependency>
  525. <groupId>net.sourceforge.jchardet</groupId>
  526. <artifactId>jchardet</artifactId>
  527. <version>${jchardet.version}</version>
  528. </dependency>
  529. <dependency>
  530. <groupId>xml-apis</groupId>
  531. <artifactId>xml-apis</artifactId>
  532. <version>1.4.01</version>
  533. </dependency>
  534. <dependency>
  535. <groupId>org.springframework.security</groupId>
  536. <artifactId>spring-security-taglibs</artifactId>
  537. <version>3.1.4.RELEASE</version>
  538. </dependency>
  539.  
  540. <dependency>
  541. <groupId>org.apache.poi</groupId>
  542. <artifactId>poi-ooxml</artifactId>
  543. <version>3.9</version>
  544. <exclusions>
  545. <exclusion>
  546. <artifactId>xml-apis</artifactId>
  547. <groupId>xml-apis</groupId>
  548. </exclusion>
  549. </exclusions>
  550. </dependency>
  551. </dependencies>
  552. <build>
  553. <finalName>springSecurity</finalName>
  554. <plugins>
  555.  
  556. <plugin>
  557. <groupId>org.mortbay.jetty</groupId>
  558. <artifactId>maven-jetty-plugin</artifactId>
  559. <configuration>
  560. <scanIntervalSeconds>10</scanIntervalSeconds>
  561. <webAppConfig>
  562. <contextPath>/springSecurity</contextPath>
  563. </webAppConfig>
  564. </configuration>
  565. </plugin>
  566. </plugins>
  567. </build>
  568. </project>

applicationContext-dao.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  12. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
  13. default-lazy-init="true">
  14.  
  15. <description>Spring DAO Configration</description>
  16.  
  17. <!-- Spring Data Jpa配置 -->
  18. <jpa:repositories base-package="com.wiker"
  19. transaction-manager-ref="transactionManager"
  20. entity-manager-factory-ref="entityManagerFactory" />
  21.  
  22. <bean id="dataSource"
  23. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  24. <!-- Connection Info -->
  25. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  26. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
  27. <property name="username" value="root" />
  28. <property name="password" value="root" />
  29. </bean>
  30.  
  31. <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->
  32. <bean id="entityManagerFactory"
  33. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  34. <property name="dataSource" ref="dataSource" />
  35. <property name="packagesToScan">
  36. <list>
  37. <value>com.wiker</value>
  38. </list>
  39. </property>
  40. <property name="jpaVendorAdapter">
  41. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  42. <property name="database" value="MYSQL" />
  43. <property name="showSql" value="true" />
  44. <property name="generateDdl" value="true" />
  45. </bean>
  46. </property>
  47. </bean>
  48.  
  49. <context:component-scan base-package="com.wiker"></context:component-scan>
  50.  
  51. <context:annotation-config />
  52.  
  53. <!-- Jpa 事务配置 -->
  54. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  55. <property name="entityManagerFactory" ref="entityManagerFactory" />
  56. </bean>
  57.  
  58. <!-- 使用annotation定义事务 -->
  59. <tx:annotation-driven transaction-manager="transactionManager"
  60. proxy-target-class="true" />
  61.  
  62. </beans>

applicationContext-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  12. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
  13. default-lazy-init="true">
  14.  
  15. <description>Spring Service Configration</description>
  16.  
  17. <!-- scan service class with @components -->
  18. <context:component-scan base-package="com.wiker.security.service" />
  19.  
  20. <bean id="propertyConfigurer"
  21. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  22. <property name="locations">
  23. <list>
  24. <value>classpath:application.properties</value>
  25. </list>
  26. </property>
  27. </bean>
  28.  
  29. <!-- <bean name="propertyLoader"
  30. class="com.urbanmania.spring.beans.factory.config.annotations.PropertyFileLoader">
  31. <property name="resources">
  32. <value>classpath:application.properties</value>
  33. </property>
  34. </bean>
  35.  
  36. <bean
  37. class="com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurer">
  38. <property name="propertyLoaders">
  39. <ref bean="propertyLoader" />
  40. </property>
  41. </bean> -->
  42. </beans>

spring-security.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/security
  7. http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  8.  
  9. <!-- 启用注解方式对方法的权限控制 -->
  10. <security:global-method-security
  11. pre-post-annotations="enabled" secured-annotations="enabled"
  12. jsr250-annotations="enabled" proxy-target-class="true">
  13. <!-- <security:protect-pointcut access="VIP"
  14. expression="execution(* com.zf.service.VipService.*(..))" /> -->
  15. </security:global-method-security>
  16.  
  17. <!--use-expressions="true" 的意思是开启表达式 access-denied-page的意思是,当验证权限失败后会跳转到的页面 -->
  18. <security:http use-expressions="true" access-denied-page="/powermiss.jsp" entry-point-ref="authenticationProcessingFilterEntryPoint">
  19. <!-- 对登录页面,所有的用户都可以访问 -->
  20. <security:intercept-url pattern="/login.jsp*"
  21. access="permitAll" />
  22. <security:intercept-url pattern="/index.jsp*"
  23. access="permitAll" />
  24. <!-- <security:intercept-url pattern="/vip.jsp*"
  25. access="hasRole('VIP')" />
  26. <security:intercept-url pattern="/admin.jsp*"
  27. access="hasRole('ADMIN')" />
  28. 对所有的资源,都必须要有COMM权限 才可以访问
  29. <security:intercept-url pattern="/*"
  30. access="hasRole('COMM')" /> -->
  31.  
  32. <!-- 使用自己的过滤器 -->
  33. <!-- 下面的配置表示将自己的过滤器放在FORM_LOGIN_FILTER过滤链的最前面(可以这样来验证登录验证码) -->
  34. <security:custom-filter
  35. ref="validateCodeAuthenticationFilter" position="FORM_LOGIN_FILTER"
  36. />
  37.  
  38. <!-- 配置登录页面为login.jsp ,登录成功默认跳转到index.jsp,登录失败返回login.jsp并携带参数error=true -->
  39. <!-- <security:form-login login-page="/login.jsp" default-target-url="/index.jsp" authentication-failure-url="/login.jsp?error=true" /> -->
  40.  
  41. <!-- 退出配置 -->
  42. <security:logout invalidate-session="true"
  43. logout-success-url="/login.jsp" logout-url="/auth/logout" />
  44.  
  45. </security:http>
  46.  
  47. <bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
  48. <property name="loginFormUrl" value="/login.jsp"></property>
  49. </bean>
  50.  
  51. <!-- 验证码过滤器 -->
  52. <bean id="validateCodeAuthenticationFilter"
  53. class="com.wiker.security.controller.VolidateAuthCodeUsernamePasswordAuthenticationFilter">
  54. <property name="authenticationSuccessHandler"
  55. ref="loginLogAuthenticationSuccessHandler"></property>
  56. <property name="authenticationFailureHandler"
  57. ref="simpleUrlAuthenticationFailureHandler"></property>
  58. <property name="authenticationManager" ref="authenticationManager"></property>
  59. </bean>
  60.  
  61. <!-- 登录成功 -->
  62. <bean id="loginLogAuthenticationSuccessHandler"
  63. class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
  64. <property name="defaultTargetUrl" value="/index.jsp"></property>
  65. </bean>
  66.  
  67. <!-- 登录失败 -->
  68. <bean id="simpleUrlAuthenticationFailureHandler"
  69. class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
  70. <property name="defaultFailureUrl" value="/login.jsp?error=true"></property>
  71. </bean>
  72.  
  73. <!-- 配置一个认证管理器 -->
  74. <security:authentication-manager alias="authenticationManager">
  75. <!-- 使用自定义的UserDetailService -->
  76. <security:authentication-provider
  77. user-service-ref="accountService">
  78. <!-- 下面的内容就可注释掉了 -->
  79. <!-- <security:user-service> -->
  80. <!-- 这样的配置表示向系统中添加了一个用户 用户名和密码都为admin ,并且该用户拥有ROLE_USER角色(角色可以用逗号隔开) -->
  81. <!-- <security:user name="admin" password="admin" authorities="ROLE_USER"/> -->
  82. <!-- </security:user-service> -->
  83. </security:authentication-provider>
  84. </security:authentication-manager>
  85.  
  86. </beans>

spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  8.  
  9. <!-- 自动扫描且只扫描@Controller -->
  10. <context:component-scan base-package="com.wiker.security.controller"
  11. use-default-filters="false">
  12. <context:include-filter type="annotation"
  13. expression="org.springframework.stereotype.Controller" />
  14. </context:component-scan>
  15.  
  16. <!-- Application Message Bundle -->
  17. <bean id="messageSource"
  18. class="org.springframework.context.support.ResourceBundleMessageSource">
  19. <property name="basename" value="messages" />
  20. </bean>
  21.  
  22. <!-- Json返回 乱码处理 -->
  23. <bean
  24. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  25. <property name="messageConverters">
  26. <list>
  27. <bean
  28. class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
  29. <bean
  30. class="org.springframework.http.converter.StringHttpMessageConverter">
  31. <property name="supportedMediaTypes">
  32. <list>
  33. <value>text/plain;charset=UTF-8</value>
  34. </list>
  35. </property>
  36. </bean>
  37. <bean
  38. class="org.springframework.http.converter.ResourceHttpMessageConverter" />
  39. <bean
  40. class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
  41. <bean
  42. class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
  43. <bean
  44. class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
  45. </list>
  46. </property>
  47. </bean>
  48.  
  49. <mvc:annotation-driven />
  50.  
  51. <!-- 将无法mapping到Controller的path交给default servlet handler处理 -->
  52. <mvc:default-servlet-handler />
  53.  
  54. <!-- 定义JSP文件的位置 -->
  55. <bean
  56. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  57. <property name="prefix" value="/" />
  58. <property name="suffix" value=".jsp" />
  59. </bean>
  60.  
  61. <!-- Configure the multipart resolver for uploading -->
  62. <bean id="multipartResolver"
  63. class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
  64.  
  65. </beans>

web.xml

  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_2_5.xsd"
  5. version="2.5">
  6. <context-param>
  7. <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
  8. <param-value>messages</param-value>
  9. </context-param>
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>
  13. classpath*:/applicationContext-dao.xml,
  14. classpath*:/applicationContext-service.xml,
  15. classpath*:/spring-security.xml
  16. </param-value>
  17. </context-param>
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <filter>
  22. <filter-name>encodingFilter</filter-name>
  23. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  24. <init-param>
  25. <param-name>encoding</param-name>
  26. <param-value>UTF-8</param-value>
  27. </init-param>
  28. <init-param>
  29. <param-name>forceEncoding</param-name>
  30. <param-value>true</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>encodingFilter</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37.  
  38. <servlet>
  39. <servlet-name>springServlet</servlet-name>
  40. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  41. <init-param>
  42. <param-name>contextConfigLocation</param-name>
  43. <param-value>/WEB-INF/spring-mvc.xml</param-value>
  44. </init-param>
  45. <load-on-startup>1</load-on-startup>
  46. </servlet>
  47. <servlet>
  48. <servlet-name>CaptchaServlet</servlet-name>
  49. <servlet-class>
  50. com.wiker.security.controller.CaptchaServlet
  51. </servlet-class>
  52. </servlet>
  53. <servlet-mapping>
  54. <servlet-name>CaptchaServlet</servlet-name>
  55. <url-pattern>/captchaServlet</url-pattern>
  56. </servlet-mapping>
  57. <servlet-mapping>
  58. <servlet-name>springServlet</servlet-name>
  59. <url-pattern>/</url-pattern>
  60. </servlet-mapping>
  61.  
  62. <!-- spring security -->
  63. <filter>
  64. <filter-name>springSecurityFilterChain</filter-name>
  65. <filter-class>
  66. org.springframework.web.filter.DelegatingFilterProxy
  67. </filter-class>
  68. </filter>
  69.  
  70. <filter-mapping>
  71. <filter-name>springSecurityFilterChain</filter-name>
  72. <url-pattern>/*</url-pattern>
  73. </filter-mapping>
  74.  
  75. </web-app>

index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12.  
  13. <title>My JSP 'index.jsp' starting page</title>
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22. </head>
  23.  
  24. <body>
  25. <h3>登录成功,欢迎您:<sec:authentication property="name" /></h3>
  26. <a href="<%=basePath%>admin.jsp">进入管理员页面</a>
  27. <a href="<%=basePath%>vip.jsp">进入会员页面</a>
  28. <a href="<%=basePath%>auth/logout">注销</a>
  29. </body>
  30. </html>

index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme() + "://" + request.getServerName() + ":"
  6. + request.getServerPort() + path + "/";
  7.  
  8. %>
  9.  
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  11. <html>
  12. <head>
  13. <base href="<%=basePath%>">
  14.  
  15. <title>My JSP 'index.jsp' starting page</title>
  16. <meta http-equiv="pragma" content="no-cache">
  17. <meta http-equiv="cache-control" content="no-cache">
  18. <meta http-equiv="expires" content="0">
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  20. <meta http-equiv="description" content="This is my page">
  21. <!--
  22. <link rel="stylesheet" type="text/css" href="styles.css">
  23. -->
  24. <style type="text/css">
  25. div.error {
  26. width: 260px;
  27. border: 2px solid red;
  28. background-color: yellow;
  29. text-align: center;
  30. }
  31.  
  32. div.hide {
  33. display: none;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="error ${param.error == true ? '' : 'hide'}">
  39. 登陆失败<br>
  40. ${sessionScope['SPRING_SECURITY_LAST_EXCEPTION'].message}
  41. </div>
  42. <h3>用户登录</h3>
  43. <!-- from的action地址,以及用户名密码的name 。都是spring-security固定的。 -->
  44. <form action="<%=basePath%>j_spring_security_check" method="post">
  45.  
  46. <p>
  47. <label for="j_username">Username</label> <input id="j_username"
  48. name="j_username" type="text" />
  49. </p>
  50.  
  51. <p>
  52. <label for="j_password">Password</label> <input id="j_password"
  53. name="j_password" type="password" />
  54. </p>
  55. <p>
  56. <label for="j_password">验证码:</label> <input id="j_password"
  57. name="code" type="password" /> <IMG style="CURSOR: pointer"
  58. onclick="this.src='captchaServlet?t='+(new Date().getTime());"
  59. alt="看不清楚?请点击刷新验证码!" align='absmiddle' src="captchaServlet"
  60. height="18" width="55">
  61. </p>
  62. <p>
  63. <input type="checkbox" name="_spring_security_remember_me">两周之内不必登陆
  64. </p>
  65.  
  66. <input type="submit" value="Login" />
  67.  
  68. </form>
  69.  
  70. </body>
  71. </html>

powermiss.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>My JSP 'powermiss.jsp' starting page</title>
  13.  
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22.  
  23. </head>
  24.  
  25. <body>
  26.  
  27. <h1 style="color: red;">对不起,您无权访问该资源!</h1>
  28.  
  29. </body>
  30. </html>

VolidateAuthCodeUsernamePasswordAuthenticationFilter.java

  1. package com.wiker.security.controller;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6.  
  7. import org.apache.commons.lang.StringUtils;
  8. import org.springframework.security.authentication.AuthenticationServiceException;
  9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.AuthenticationException;
  12. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  13. import org.springframework.security.web.util.TextEscapeUtils;
  14.  
  15. /**
  16. * @author Wiker Yong Email:<a href="mailto:wikeryong@gmail.com">wikeryong@gmail.com</a>
  17. * @date 2013-7-15 下午5:56:54
  18. * @version 1.0-SNAPSHOT
  19. */
  20. public class VolidateAuthCodeUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
  21.  
  22. private boolean postOnly = true;
  23. private boolean allowEmptyValidateCode = false;
  24. private String sessionvalidateCodeField = DEFAULT_SESSION_VALIDATE_CODE_FIELD;
  25. private String validateCodeParameter = DEFAULT_VALIDATE_CODE_PARAMETER;
  26. public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
  27. // session中保存的验证码
  28. public static final String DEFAULT_SESSION_VALIDATE_CODE_FIELD = "rand";
  29. // 输入的验证码
  30. public static final String DEFAULT_VALIDATE_CODE_PARAMETER = "code";
  31.  
  32. @Override
  33. public Authentication attemptAuthentication(HttpServletRequest request,
  34. HttpServletResponse response) throws AuthenticationException {
  35. if (postOnly && !request.getMethod().equals("POST")) {
  36. throw new AuthenticationServiceException(
  37. "Authentication method not supported: "
  38. + request.getMethod());
  39. }
  40.  
  41. String username = obtainUsername(request);
  42. String password = obtainPassword(request);
  43.  
  44. if (username == null) {
  45. username = "";
  46. }
  47.  
  48. if (password == null) {
  49. password = "";
  50. }
  51.  
  52. username = username.trim();
  53.  
  54. UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
  55. username, password);
  56.  
  57. // Place the last username attempted into HttpSession for views
  58. HttpSession session = request.getSession(false);
  59.  
  60. if (session != null || getAllowSessionCreation()) {
  61. request.getSession().setAttribute(
  62. SPRING_SECURITY_LAST_USERNAME_KEY,
  63. TextEscapeUtils.escapeEntities(username));
  64. }
  65.  
  66. // Allow subclasses to set the "details" property
  67. setDetails(request, authRequest);
  68. // check validate code
  69. if (!isAllowEmptyValidateCode())
  70. checkValidateCode(request);
  71. // 根据用户和密码查询
  72.  
  73. return this.getAuthenticationManager().authenticate(authRequest);
  74. }
  75.  
  76. /**
  77. *
  78. * <li>比较session中的验证码和用户输入的验证码是否相等</li>
  79. *
  80. */
  81. protected void checkValidateCode(HttpServletRequest request) {
  82. String sessionValidateCode = obtainSessionValidateCode(request);
  83. String validateCodeParameter = obtainValidateCodeParameter(request);
  84. if (StringUtils.isEmpty(validateCodeParameter)
  85. || !sessionValidateCode.equalsIgnoreCase(validateCodeParameter)) {
  86. throw new AuthenticationServiceException("验证码错误!");
  87. }
  88. }
  89.  
  90. private String obtainValidateCodeParameter(HttpServletRequest request) {
  91. return request.getParameter(validateCodeParameter);
  92. }
  93.  
  94. protected String obtainSessionValidateCode(HttpServletRequest request) {
  95. Object obj = request.getSession()
  96. .getAttribute(sessionvalidateCodeField);
  97. return null == obj ? "" : obj.toString();
  98. }
  99.  
  100. public boolean isPostOnly() {
  101. return postOnly;
  102. }
  103.  
  104. @Override
  105. public void setPostOnly(boolean postOnly) {
  106. this.postOnly = postOnly;
  107. }
  108.  
  109. public String getValidateCodeName() {
  110. return sessionvalidateCodeField;
  111. }
  112.  
  113. public void setValidateCodeName(String validateCodeName) {
  114. this.sessionvalidateCodeField = validateCodeName;
  115. }
  116.  
  117. public boolean isAllowEmptyValidateCode() {
  118. return allowEmptyValidateCode;
  119. }
  120.  
  121. public void setAllowEmptyValidateCode(boolean allowEmptyValidateCode) {
  122. this.allowEmptyValidateCode = allowEmptyValidateCode;
  123. }
  124.  
  125. }

User.java

  1. package com.wiker.security.dao.entity;
  2.  
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import javax.persistence.Entity;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.GenerationType;
  10. import javax.persistence.Id;
  11. import javax.persistence.JoinColumn;
  12. import javax.persistence.JoinTable;
  13. import javax.persistence.ManyToMany;
  14. import javax.persistence.Table;
  15.  
  16. /**
  17. * 用户Bean
  18. * @author WikerYong <a>Email:yw_312@foxmail.com<a/>
  19. * @version 2011-11-9 上午10:03:18
  20. */
  21. @Entity
  22. @Table(name = "user")
  23. public class User implements Serializable{
  24.  
  25. protected static final long serialVersionUID = -5204668503508016656L;
  26.  
  27. @Id
  28. @GeneratedValue(strategy = GenerationType.AUTO)
  29. protected Long id;
  30. protected String username;
  31. protected String password;
  32. @ManyToMany
  33. @JoinTable(name="user_role" , joinColumns = {
  34. @JoinColumn(name = "userid")
  35. }, inverseJoinColumns = {@JoinColumn(name="roleid")})
  36. private List<Role> roles = new ArrayList<Role>();
  37. public Long getId() {
  38. return id;
  39. }
  40. public void setId(Long id) {
  41. this.id = id;
  42. }
  43. public String getUsername() {
  44. return username;
  45. }
  46. public void setUsername(String username) {
  47. this.username = username;
  48. }
  49. public String getPassword() {
  50. return password;
  51. }
  52. public void setPassword(String password) {
  53. this.password = password;
  54. }
  55. public List<Role> getRoles() {
  56. return roles;
  57. }
  58. public void setRoles(List<Role> roles) {
  59. this.roles = roles;
  60. }
  61. }

UserDao.java

  1. package com.wiker.security.dao.repository;
  2.  
  3. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  4. import org.springframework.data.repository.PagingAndSortingRepository;
  5.  
  6. import com.wiker.security.dao.entity.User;
  7.  
  8. /**
  9. * @author Wiker Yong Email:<a href="mailto:wikeryong@gmail.com">wikeryong@gmail.com</a>
  10. * @date 2013-6-19 上午11:11:52
  11. * @version 1.0-SNAPSHOT
  12. */
  13. public interface UserDao extends PagingAndSortingRepository<User,Long>,JpaSpecificationExecutor<User> {
  14.  
  15. User findByUsernameAndPassword(String username,String password);
  16.  
  17. User findByUsername(String username);
  18.  
  19. }

AccountService.java

  1. package com.wiker.security.service;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.core.GrantedAuthority;
  9. import org.springframework.security.core.authority.GrantedAuthorityImpl;
  10. import org.springframework.security.core.userdetails.UserDetails;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  13. import org.springframework.stereotype.Service;
  14.  
  15. import com.wiker.security.dao.entity.User;
  16. import com.wiker.security.dao.repository.UserDao;
  17.  
  18. @Service
  19. public class AccountService
  20. implements UserDetailsService {
  21.  
  22. @Autowired
  23. private UserDao userDao;
  24.  
  25. public UserDetails loadUserByUsername(String username)
  26. throws UsernameNotFoundException {
  27. User user = userDao.findByUsername(username);
  28. UserDetails userDetail = null;
  29. if (user != null) {
  30. userDetail = new org.springframework.security.core.userdetails.User(username,
  31. user.getPassword(), true, true, true, true, findUserAuthorities(user));
  32. }
  33.  
  34. return userDetail;
  35. }
  36.  
  37. /**
  38. * 获取用户的权限
  39. *
  40. * @param user
  41. * @return
  42. */
  43. @SuppressWarnings("deprecation")
  44. public Collection<GrantedAuthority> findUserAuthorities(User user) {
  45. List<GrantedAuthority> autthorities = new ArrayList<GrantedAuthority>();
  46. /*List<Role> roles = user.getRoles();
  47. for (Role Role : roles) {
  48. autthorities.add(new GrantedAuthorityImpl(Role.getRoleCode()));
  49. }*/
  50. autthorities.add(new GrantedAuthorityImpl("admin"));
  51. return autthorities;
  52. }
  53.  
  54. }

Spring Security3实现,权限动态获取的更多相关文章

  1. JavaEE学习之Spring Security3.x——模拟数据库实现用户,权限,资源的管理

    一.引言 因项目需要最近研究了下Spring Security3.x,并模拟数据库实现用户,权限,资源的管理. 二.准备 1.了解一些Spring MVC相关知识: 2.了解一些AOP相关知识: 3. ...

  2. 动态获取Android权限

    @TargetApi(23)private void showPhotoDialog() { if (dialog != null && dialog.isShowing()) { d ...

  3. 使用Spring Security3的四种方法概述

    使用Spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过, ...

  4. (39.2). Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载) (4). 集成Shiro 进行用户授权 在看此小节前,您可能需要先看: http://412887952-qq-com.iteye.com/blog/229973 ...

  5. (39.1) Spring Boot Shiro权限管理【从零开始学Spring Boot】

    (本节提供源代码,在最下面可以下载)距上一个章节过了二个星期了,最近时间也是比较紧,一直没有时间可以写博客,今天难得有点时间,就说说Spring Boot如何集成Shiro吧.这个章节会比较复杂,牵涉 ...

  6. Spring中的JDK动态代理

    Spring中的JDK动态代理 在JDK1.3以后提供了动态代理的技术,允许开发者在运行期创建接口的代理实例.在Sun刚推出动态代理时,还很难想象它有多大的实际用途,现在动态代理是实现AOP的绝好底层 ...

  7. Spring Security3学习实例

    Spring Security是什么? Spring Security,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理 ...

  8. Spring security3

    最近一直在学习spring security3,试着搭建了环境: 构建maven环境 项目配置pom.xml文件 <project xmlns="http://maven.apache ...

  9. Spring AOP中的动态代理

    0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  Spring AOP中的动态代理机制 2.1  ...

随机推荐

  1. mybatis拦截器分页

    package com.test.interceptor; import java.sql.Connection; import java.sql.ResultSet; import java.sql ...

  2. MVC学习笔记--IEnumerable的用法

    IEnumerable的用法 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现 并支持非泛型方法的简单的 ...

  3. final使用

    final修饰  基本数据类型时候   对应的 数据不能改变:::final修饰 对象类型 ,那么对应的引用地址不能改变(对象中的值可以改变): 如果final修改方法,那么该方法不能被子类重写 :: ...

  4. Struts2第四天

    Struts2第四天 昨天: 自定义的拦截器:继续methodFilterInterceptor,可以指定哪些方法需要拦截或者不拦截. Intercepters(配置拦截器),intercepter( ...

  5. 第六十九节,css入门基础

    css入门基础 学习要点: 1.使用CSS 2.三种方式 3.层叠和继承 本章主要探讨HTML5中CSS (层叠样式表),它是用来对HTML文档外观的表现形式进行排版和格式化. 一 使用CSS CSS ...

  6. mysql 批量修改表前缀

    直接贴码: SELECT a.*, concat( 'alter table ', a.TABLE_NAME, ' rename ge_', SUBSTR( a.TABLE_NAME FROM INS ...

  7. 《高性能Javascript》读书笔记-1

    第一章 加载和执行 当浏览器执行JavaScript代码时,不能同时做其他任何事情(单一进程),意味着<script>标签每次出现都霸道地让页面等带脚本的解析和执行(每个文件必须等到前一个 ...

  8. Java 泛型 协变式覆盖和泛型重载

    Java 泛型 协变式覆盖和泛型重载 @author ixenos 1.协变式覆盖(Override) 在JDK 1.4及以前,子类方法如果要覆盖超类的某个方法,必须具有完全相同的方法签名,包括返回值 ...

  9. MongoDB数据模型(二)

    原文地址 接上一篇 四.模型树结构 父引用的模型树结构 这个数据模型描述了一个树形结构,在子节点中存储父节点的引用. 模式 父引用模式存储每个树节点到文档中,除了树节点外,文档还存储了父节点的id. ...

  10. Graph Algorithm

    1.定义 A graph consists of a set of vertices V and a set of edges E. Each edge is a pair (v, w), where ...