Skip to content

Commit d478824

Browse files
committed
spring security
spring security
1 parent 24c259f commit d478824

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-0
lines changed

spring-mvc-security/.classpath

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="var" path="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.1.0.RELEASE/spring-core-3.1.0.RELEASE.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-asm/3.1.0.RELEASE/spring-asm-3.1.0.RELEASE.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-web/3.1.0.RELEASE/spring-web-3.1.0.RELEASE.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar" sourcepath="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0-sources.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.1.0.RELEASE/spring-beans-3.1.0.RELEASE.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.1.0.RELEASE/spring-context-3.1.0.RELEASE.jar"/>
15+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.0.6.RELEASE/spring-aop-3.0.6.RELEASE.jar"/>
16+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.1.0.RELEASE/spring-expression-3.1.0.RELEASE.jar"/>
17+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-webmvc/3.1.0.RELEASE/spring-webmvc-3.1.0.RELEASE.jar"/>
18+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.1.0.RELEASE/spring-context-support-3.1.0.RELEASE.jar"/>
19+
<classpathentry kind="var" path="M2_REPO/org/springframework/security/spring-security-core/3.1.0.RELEASE/spring-security-core-3.1.0.RELEASE.jar"/>
20+
<classpathentry kind="var" path="M2_REPO/org/springframework/security/spring-security-crypto/3.1.0.RELEASE/spring-security-crypto-3.1.0.RELEASE.jar"/>
21+
<classpathentry kind="var" path="M2_REPO/org/springframework/security/spring-security-web/3.1.0.RELEASE/spring-security-web-3.1.0.RELEASE.jar"/>
22+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/3.0.6.RELEASE/spring-tx-3.0.6.RELEASE.jar"/>
23+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-jdbc/3.0.6.RELEASE/spring-jdbc-3.0.6.RELEASE.jar"/>
24+
<classpathentry kind="var" path="M2_REPO/org/springframework/security/spring-security-config/3.1.0.RELEASE/spring-security-config-3.1.0.RELEASE.jar"/>
25+
</classpath>

spring-mvc-security/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>spring-mvc-security</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-mvc-security</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<spring.version>3.1.0.RELEASE</spring.version>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<!-- Spring 3.1.0 -->
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-core</artifactId>
23+
<version>${spring.version}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-web</artifactId>
29+
<version>${spring.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-webmvc</artifactId>
35+
<version>${spring.version}</version>
36+
</dependency>
37+
38+
<!-- Spring Security -->
39+
<dependency>
40+
<groupId>org.springframework.security</groupId>
41+
<artifactId>spring-security-core</artifactId>
42+
<version>${spring.version}</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.security</groupId>
47+
<artifactId>spring-security-web</artifactId>
48+
<version>${spring.version}</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.springframework.security</groupId>
53+
<artifactId>spring-security-config</artifactId>
54+
<version>${spring.version}</version>
55+
</dependency>
56+
57+
<!-- Java Servlet API 3.1 -->
58+
<dependency>
59+
<groupId>javax.servlet</groupId>
60+
<artifactId>javax.servlet-api</artifactId>
61+
<version>3.1.0</version>
62+
</dependency>
63+
64+
</dependencies>
65+
<build>
66+
<finalName>spring-mvc-security</finalName>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.eclipse.jetty</groupId>
70+
<artifactId>jetty-maven-plugin</artifactId>
71+
<version>9.0.4.v20130625</version>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hmkcode.controllers;
2+
3+
4+
import org.springframework.security.core.context.SecurityContextHolder;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.ModelMap;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
10+
@Controller
11+
@RequestMapping("/controller")
12+
public class RestController {
13+
14+
public RestController(){
15+
System.out.println("init RestController");
16+
}
17+
18+
// URL: /rest/controller/any
19+
@RequestMapping(value="/any", method = RequestMethod.GET)
20+
public String any(ModelMap model) {
21+
22+
model.addAttribute("message", "This URL can be accessed by any one");
23+
return "hello";
24+
25+
}
26+
27+
// URL: /rest/controller/user
28+
@RequestMapping(value="/user", method = RequestMethod.GET)
29+
public String user(ModelMap model) {
30+
String user = SecurityContextHolder.getContext().getAuthentication().getName();
31+
32+
model.addAttribute("message", "This URL can be accessed only by authorized user "+user);
33+
34+
return "hello";
35+
}
36+
37+
}
38+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context-3.0.xsd
9+
http://www.springframework.org/schema/mvc
10+
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
11+
12+
<context:component-scan base-package="com.hmkcode.controllers" />
13+
14+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15+
<property name="prefix">
16+
<value>/</value>
17+
</property>
18+
<property name="suffix">
19+
<value>.jsp</value>
20+
</property>
21+
</bean>
22+
</beans>
23+
24+
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<bean:beans xmlns:bean="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns="http://www.springframework.org/schema/security"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
7+
http://www.springframework.org/schema/security
8+
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
9+
10+
<http auto-config="true">
11+
<intercept-url pattern="/rest/controller/any/**" access="ROLE_ANONYMOUS,ROLE_USER"/>
12+
13+
<intercept-url pattern="/rest/controller/user/**" access="ROLE_USER" />
14+
</http>
15+
16+
<authentication-manager>
17+
<authentication-provider>
18+
<user-service>
19+
<user name="hmkcode" password="1234" authorities="ROLE_USER" />
20+
<user name="user2" password="1234" authorities="ROLE_ANONYMOUS" />
21+
</user-service>
22+
</authentication-provider>
23+
</authentication-manager>
24+
25+
</bean:beans>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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"
4+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6+
id="WebApp_ID" version="3.0">
7+
<display-name>SPRING MVC SECURITY</display-name>
8+
<welcome-file-list>
9+
<welcome-file>index.html</welcome-file>
10+
</welcome-file-list>
11+
12+
<!-- Spring MVC -->
13+
<servlet>
14+
<servlet-name>rest</servlet-name>
15+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16+
</servlet>
17+
<servlet-mapping>
18+
<servlet-name>rest</servlet-name>
19+
<url-pattern>/rest/*</url-pattern>
20+
</servlet-mapping>
21+
22+
<listener>
23+
<listener-class>
24+
org.springframework.web.context.ContextLoaderListener
25+
</listener-class>
26+
</listener>
27+
28+
<context-param>
29+
<param-name>contextConfigLocation</param-name>
30+
<param-value>
31+
/WEB-INF/rest-servlet.xml,
32+
/WEB-INF/spring-security.xml
33+
</param-value>
34+
</context-param>
35+
36+
<!-- Spring Security -->
37+
<filter>
38+
<filter-name>springSecurityFilterChain</filter-name>
39+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
40+
</filter>
41+
42+
<filter-mapping>
43+
<filter-name>springSecurityFilterChain</filter-name>
44+
<url-pattern>/*</url-pattern>
45+
</filter-mapping>
46+
47+
</web-app>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>Message : ${message}</h1>
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)