Skip to content

Commit 92a6624

Browse files
committed
Spring boot project security db
1 parent b4cd94a commit 92a6624

File tree

25 files changed

+1199
-62
lines changed

25 files changed

+1199
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
//package spring.project.controller;
2-
//
3-
//import org.springframework.security.core.Authentication;
4-
//import org.springframework.security.core.context.SecurityContextHolder;
5-
//import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
6-
//import org.springframework.stereotype.Controller;
7-
//import org.springframework.web.bind.annotation.RequestMapping;
8-
//import org.springframework.web.bind.annotation.RequestMethod;
9-
//import org.springframework.web.bind.annotation.RestController;
10-
//
11-
//import javax.servlet.http.HttpServletRequest;
12-
//import javax.servlet.http.HttpServletResponse;
13-
//
14-
///**
15-
// * @Created 28 / 04 / 2020 - 3:21 PM
16-
// * @project SpringToDoSimple
17-
// * @Author Hamdamboy
18-
// */
19-
//@Controller
20-
//public class LogoutController {
21-
//
22-
// @RequestMapping(value = "/logout", method = RequestMethod.GET)
23-
// public String logout(HttpServletRequest request, HttpServletResponse response) {
24-
//
25-
// Authentication authencation = SecurityContextHolder.getContext()
26-
// .getAuthentication();
27-
//
28-
// if(authencation != null) {
29-
// new SecurityContextLogoutHandler().logout(request, response, authencation);
30-
// }
31-
//
32-
// return "redirect:/";
33-
// }
34-
//}
2+
3+
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.core.context.SecurityContextHolder;
5+
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
/**
14+
* @Created 28 / 04 / 2020 - 3:21 PM
15+
* @project SpringToDoSimple
16+
* @Author Hamdamboy
17+
*/
18+
@Controller
19+
public class LogoutController {
20+
21+
@RequestMapping(value = "/logout", method = RequestMethod.GET)
22+
public String logout(HttpServletRequest request, HttpServletResponse response) {
23+
24+
Authentication authencation = SecurityContextHolder.getContext()
25+
.getAuthentication();
26+
27+
if(authencation != null) {
28+
new SecurityContextLogoutHandler().logout(request, response, authencation);
29+
}
30+
31+
return "redirect:/";
32+
}
33+
}

Part-8 Spring Boot Real Projects/0.ToDoProjectSimple/src/main/java/spring/project/controller/TodoController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.springframework.security.core.context.SecurityContextHolder;
66
import org.springframework.security.core.userdetails.UserDetails;
77
import org.springframework.stereotype.Controller;
8-
import org.springframework.ui.Model;
98
import org.springframework.ui.ModelMap;
109
import org.springframework.validation.BindingResult;
1110
import org.springframework.web.bind.WebDataBinder;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package spring.project.controller;
22

3+
import org.springframework.security.core.context.SecurityContextHolder;
4+
import org.springframework.security.core.userdetails.UserDetails;
35
import org.springframework.stereotype.Controller;
46
import org.springframework.ui.ModelMap;
57
import org.springframework.web.bind.annotation.GetMapping;
@@ -20,20 +22,20 @@ public class WelcomeController {
2022
public String showWelcomePage(){
2123
return "welcome";
2224
}
23-
// public String showWelcomePage(ModelMap modelMap) {
24-
// modelMap.put("name", getLoggedinUserName());
25-
// return "welcome";
26-
// }
25+
public String showWelcomePage(ModelMap modelMap) {
26+
modelMap.put("name", getLoggedinUserName());
27+
return "welcome";
28+
}
29+
30+
private String getLoggedinUserName() {
31+
Object principal = SecurityContextHolder.getContext()
32+
.getAuthentication().getPrincipal();
2733

28-
// private String getLoggedinUserName() {
29-
// Object principal = SecurityContextHolder.getContext()
30-
// .getAuthentication().getPrincipal();
31-
//
32-
// if(principal instanceof UserDetails) {
33-
// return ((UserDetails) principal).getUsername();
34-
// }
35-
//
36-
// return principal.toString();
37-
// }
34+
if(principal instanceof UserDetails) {
35+
return ((UserDetails) principal).getUsername();
36+
}
37+
38+
return principal.toString();
39+
}
3840

3941
}

Part-8 Spring Boot Real Projects/0.ToDoProjectSimple/src/main/java/spring/project/security/SecurityConfiguration.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package spring.project.security;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
65
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
76
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
87
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
108
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
11-
import org.springframework.security.crypto.password.PasswordEncoder;
129

1310
/**
1411
* @Created 28 / 04 / 2020 - 11:24 AM

Part-8 Spring Boot Real Projects/2.TodoSimpleProject/src/main/java/spring/project/controller/ToDoController.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public String index(){
2727
return "index";
2828
}
2929

30+
@GetMapping("/logout")
31+
public String logoutPage(){
32+
return "logout";
33+
}
34+
3035
@GetMapping("/todos")
3136
public String todos(Model model){
3237
model.addAttribute("todos", todoRepository.findAll());
@@ -44,11 +49,11 @@ public String add(@RequestParam String todoItem, @RequestParam String status, Mo
4449
}
4550

4651
@PostMapping("/todoDelete/{id}")
47-
public String delete(@RequestParam long id, Model model){
52+
public String delete(@PathVariable long id, Model model){
4853
todoRepository.deleteById(id);
4954
model.addAttribute("todos", todoRepository.findAll());
5055

51-
return "redirect/:todos";
56+
return "redirect:/todos";
5257
}
5358

5459
@PostMapping("/todoUpdate/{id}")
@@ -59,14 +64,8 @@ public String update(@PathVariable long id, Model model){
5964
} else {
6065
toDo.setCompleted("Yes");
6166
}
62-
6367
todoRepository.save(toDo);
6468
model.addAttribute("todos", todoRepository.findAll());
6569
return "redirect:/todos";
66-
67-
68-
69-
70-
7170
}
7271
}

Part-8 Spring Boot Real Projects/2.TodoSimpleProject/src/main/java/spring/project/repository/ToDoRepository.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@
1212

1313
@Repository
1414
public interface ToDoRepository extends PagingAndSortingRepository<ToDo, Long> {
15-
16-
17-
// void addAttribute(String todos, Iterable<ToDo> all);
1815
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Index Page</title>
8+
<style type="text/css">
9+
#todocontainer {
10+
margin-top: 50px;
11+
}
12+
</style>
13+
<link rel="stylesheet"
14+
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
15+
</head>
16+
<body>
17+
<section id="header">
18+
<nav class="navbar navbar-expand-lg navbar-light bg-light">
19+
<a class="navbar-brand" href="#">TODOS</a>
20+
<button class="navbar-toggler" type="button" data-toggle="collapse"
21+
data-target="#navbarSupportedContent"
22+
aria-controls="navbarSupportedContent" aria-expanded="false"
23+
aria-label="Toggle navigation">
24+
<span class="navbar-toggler-icon"></span>
25+
</button>
26+
</nav>
27+
</section>
28+
<section id="todocontainer">
29+
<h2 style="padding-top: 30px; padding-bottom: 20px;" class="text-center">You come again todo manager page</h2>
30+
<div class="d-flex justify-content-center">
31+
<form th:action="@{/todos}" method="GET" enctype="multipart/form-data">
32+
<div class="form-group">
33+
<button type="submit" class="btn btn-success btn-lg text-white">View Todos</button>
34+
</div>
35+
</form>
36+
</div>
37+
38+
</section>
39+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
40+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
41+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
42+
</body>
43+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.net.*;
18+
import java.io.*;
19+
import java.nio.channels.*;
20+
import java.util.Properties;
21+
22+
public class MavenWrapperDownloader {
23+
24+
private static final String WRAPPER_VERSION = "0.5.6";
25+
/**
26+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
27+
*/
28+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
30+
31+
/**
32+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
33+
* use instead of the default one.
34+
*/
35+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
36+
".mvn/wrapper/maven-wrapper.properties";
37+
38+
/**
39+
* Path where the maven-wrapper.jar will be saved to.
40+
*/
41+
private static final String MAVEN_WRAPPER_JAR_PATH =
42+
".mvn/wrapper/maven-wrapper.jar";
43+
44+
/**
45+
* Name of the property which should be used to override the default download url for the wrapper.
46+
*/
47+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
48+
49+
public static void main(String args[]) {
50+
System.out.println("- Downloader started");
51+
File baseDirectory = new File(args[0]);
52+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
53+
54+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
55+
// wrapperUrl parameter.
56+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57+
String url = DEFAULT_DOWNLOAD_URL;
58+
if (mavenWrapperPropertyFile.exists()) {
59+
FileInputStream mavenWrapperPropertyFileInputStream = null;
60+
try {
61+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62+
Properties mavenWrapperProperties = new Properties();
63+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65+
} catch (IOException e) {
66+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67+
} finally {
68+
try {
69+
if (mavenWrapperPropertyFileInputStream != null) {
70+
mavenWrapperPropertyFileInputStream.close();
71+
}
72+
} catch (IOException e) {
73+
// Ignore ...
74+
}
75+
}
76+
}
77+
System.out.println("- Downloading from: " + url);
78+
79+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80+
if (!outputFile.getParentFile().exists()) {
81+
if (!outputFile.getParentFile().mkdirs()) {
82+
System.out.println(
83+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84+
}
85+
}
86+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87+
try {
88+
downloadFileFromURL(url, outputFile);
89+
System.out.println("Done");
90+
System.exit(0);
91+
} catch (Throwable e) {
92+
System.out.println("- Error downloading");
93+
e.printStackTrace();
94+
System.exit(1);
95+
}
96+
}
97+
98+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100+
String username = System.getenv("MVNW_USERNAME");
101+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102+
Authenticator.setDefault(new Authenticator() {
103+
@Override
104+
protected PasswordAuthentication getPasswordAuthentication() {
105+
return new PasswordAuthentication(username, password);
106+
}
107+
});
108+
}
109+
URL website = new URL(urlString);
110+
ReadableByteChannel rbc;
111+
rbc = Channels.newChannel(website.openStream());
112+
FileOutputStream fos = new FileOutputStream(destination);
113+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114+
fos.close();
115+
rbc.close();
116+
}
117+
118+
}
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
Loading

0 commit comments

Comments
 (0)