Skip to content

Commit 177e339

Browse files
SpringBootSecurityDaoAuthenticationExample
1 parent 76a6734 commit 177e339

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+14827
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mvn clean install
2+
mvn package
3+
mvn spring-boot:run
4+
5+
http://localhost:8080/
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<groupId>SpringBootSecurityDaoAuthenticationExample</groupId>
10+
<artifactId>SpringBootSecurityDaoAuthenticationExample</artifactId>
11+
<version>1.0-SNAPSHOT</version>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.3.1.RELEASE</version>
17+
</parent>
18+
19+
<organization>
20+
<name>ESpark</name>
21+
<url>http://adarshkumarsingh83.blogspot.in/</url>
22+
</organization>
23+
24+
<licenses>
25+
<license>
26+
<name>ESpark</name>
27+
<url>http://adarshkumarsingh83.blogspot.in/licenses/LICENSE-2.0.txt</url>
28+
<distribution>repo</distribution>
29+
</license>
30+
</licenses>
31+
32+
<developers>
33+
<developer>
34+
<id>adarshkumarsingh83</id>
35+
<name>Adarsh Kumar</name>
36+
<email>adarshkumarsingh83@gmail.com</email>
37+
<roles>
38+
<role>project architect</role>
39+
</roles>
40+
</developer>
41+
</developers>
42+
43+
<repositories>
44+
<repository>
45+
<id>maven2-repository.java.net</id>
46+
<name>Java.net Repository for Maven</name>
47+
<url>http://download.java.net/maven/2/</url>
48+
</repository>
49+
50+
<repository>
51+
<id>JBoss repository</id>
52+
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
53+
</repository>
54+
55+
<repository>
56+
<id>java.net</id>
57+
<url>https://maven.java.net/content/repositories/public/</url>
58+
</repository>
59+
60+
</repositories>
61+
62+
<properties>
63+
<project.name>SpringBootSecurityDaoAuthenticationExample</project.name>
64+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
65+
<java.version>1.7</java.version>
66+
<junit.version>4.11</junit.version>
67+
<slf4j.version>1.6.1</slf4j.version>
68+
<log4j.version>1.2.17</log4j.version>
69+
</properties>
70+
71+
<dependencies>
72+
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-starter-web</artifactId>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-starter-test</artifactId>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-starter-security</artifactId>
91+
</dependency>
92+
93+
<dependency>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-starter-jdbc</artifactId>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>mysql</groupId>
100+
<artifactId>mysql-connector-java</artifactId>
101+
<version>5.1.31</version>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>commons-dbcp</groupId>
106+
<artifactId>commons-dbcp</artifactId>
107+
<version>1.4</version>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>junit</groupId>
112+
<artifactId>junit</artifactId>
113+
<version>${junit.version}</version>
114+
</dependency>
115+
116+
<dependency>
117+
<groupId>org.slf4j</groupId>
118+
<artifactId>slf4j-api</artifactId>
119+
<version>${slf4j.version}</version>
120+
</dependency>
121+
122+
<dependency>
123+
<groupId>log4j</groupId>
124+
<artifactId>log4j</artifactId>
125+
<version>${log4j.version}</version>
126+
</dependency>
127+
128+
</dependencies>
129+
130+
<build>
131+
<finalName>${project.name}</finalName>
132+
<outputDirectory>target/classes</outputDirectory>
133+
<resources>
134+
<resource>
135+
<directory>src/main/resources</directory>
136+
<filtering>true</filtering>
137+
</resource>
138+
</resources>
139+
<plugins>
140+
<plugin>
141+
<artifactId>maven-compiler-plugin</artifactId>
142+
<configuration>
143+
<source>${java.version}</source>
144+
<target>${java.version}</target>
145+
<encoding>${project.build.sourceEncoding}</encoding>
146+
</configuration>
147+
</plugin>
148+
149+
<plugin>
150+
<groupId>org.springframework.boot</groupId>
151+
<artifactId>spring-boot-maven-plugin</artifactId>
152+
</plugin>
153+
</plugins>
154+
</build>
155+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Espark nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.espark.adarsh.bean;
32+
33+
/**
34+
* @author Adarsh Kumar
35+
* @author $LastChangedBy: Adarsh Kumar$
36+
* @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$
37+
* @Espark @copyright all right reserve
38+
*/
39+
public class UserBean {
40+
41+
private String username;
42+
private String password;
43+
private String role;
44+
45+
public String getUsername() {
46+
return username;
47+
}
48+
49+
public void setUsername(String username) {
50+
this.username = username;
51+
}
52+
53+
public String getPassword() {
54+
return password;
55+
}
56+
57+
public void setPassword(String password) {
58+
this.password = password;
59+
}
60+
61+
public String getRole() {
62+
return role;
63+
}
64+
65+
public void setRole(String role) {
66+
this.role = role;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Espark nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.espark.adarsh.configuration.mvc;
32+
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
35+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
36+
37+
/**
38+
* @author Adarsh Kumar
39+
* @author $LastChangedBy: Adarsh Kumar$
40+
* @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$
41+
* @Espark @copyright all right reserve
42+
*/
43+
@Configuration
44+
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
45+
46+
@Override
47+
public void addViewControllers(ViewControllerRegistry registry) {
48+
registry.addViewController("/").setViewName("esparkHome");
49+
registry.addViewController("/esparkHome").setViewName("esparkHome");
50+
registry.addViewController("/esparkUserPage").setViewName("esparkUserPage");
51+
registry.addViewController("/esparkLoginPage").setViewName("esparkLoginPage");
52+
}
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Espark nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.espark.adarsh.configuration.security;
32+
33+
34+
import com.espark.adarsh.bean.UserBean;
35+
import com.espark.adarsh.repository.UserRepositoryImpl;
36+
import org.springframework.beans.factory.annotation.Autowired;
37+
import org.springframework.beans.factory.annotation.Qualifier;
38+
import org.springframework.security.core.GrantedAuthority;
39+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
40+
import org.springframework.security.core.userdetails.User;
41+
import org.springframework.security.core.userdetails.UserDetails;
42+
import org.springframework.security.core.userdetails.UserDetailsService;
43+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
44+
import org.springframework.stereotype.Service;
45+
46+
import java.util.Arrays;
47+
/**
48+
* @author Adarsh Kumar
49+
* @author $LastChangedBy: Adarsh Kumar$
50+
* @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$
51+
* @Espark @copyright all right reserve
52+
*/
53+
@Service
54+
public class AuthenticationService implements UserDetailsService {
55+
56+
@Qualifier(value = "userRepository")
57+
@Autowired(required = true)
58+
private UserRepositoryImpl userRepository;
59+
60+
@Override
61+
public UserDetails loadUserByUsername(String username)
62+
throws UsernameNotFoundException {
63+
UserBean userBean = userRepository.getUserInfo(username);
64+
GrantedAuthority authority = new SimpleGrantedAuthority(userBean.getRole());
65+
UserDetails userDetails = (UserDetails)new User(userBean.getUsername(),
66+
userBean.getPassword(), Arrays.asList(authority));
67+
return userDetails;
68+
}
69+
}

0 commit comments

Comments
 (0)