Skip to content

Commit 3fc3124

Browse files
committed
Some more changes
1 parent 2d87144 commit 3fc3124

8 files changed

Lines changed: 112 additions & 118 deletions

File tree

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
11
package com.springangularsecurity.model;
22

3-
import javax.persistence.*;
43
import java.util.Set;
54

5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.JoinTable;
11+
import javax.persistence.ManyToMany;
12+
import javax.persistence.Table;
13+
614
@Entity
715
@Table(name = "user")
816
public class User {
9-
private Long id;
10-
private String username;
11-
private String password;
12-
private String passwordConfirm;
13-
private Set<Role> roles;
14-
15-
@Id
16-
@GeneratedValue(strategy = GenerationType.AUTO)
17-
public Long getId() {
18-
return id;
19-
}
20-
21-
public void setId(Long id) {
22-
this.id = id;
23-
}
24-
25-
public String getUsername() {
26-
return username;
27-
}
28-
29-
public void setUsername(String username) {
30-
this.username = username;
31-
}
32-
33-
public String getPassword() {
34-
return password;
35-
}
36-
37-
public void setPassword(String password) {
38-
this.password = password;
39-
}
40-
41-
@Transient
42-
public String getPasswordConfirm() {
43-
return passwordConfirm;
44-
}
45-
46-
public void setPasswordConfirm(String passwordConfirm) {
47-
this.passwordConfirm = passwordConfirm;
48-
}
49-
50-
@ManyToMany
51-
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
52-
public Set<Role> getRoles() {
53-
return roles;
54-
}
55-
56-
public void setRoles(Set<Role> roles) {
57-
this.roles = roles;
58-
}
17+
private Long id;
18+
private String username;
19+
private String password;
20+
private Set<Role> roles;
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.AUTO)
24+
public Long getId() {
25+
return id;
26+
}
27+
28+
public void setId(Long id) {
29+
this.id = id;
30+
}
31+
32+
public String getUsername() {
33+
return username;
34+
}
35+
36+
public void setUsername(String username) {
37+
this.username = username;
38+
}
39+
40+
public String getPassword() {
41+
return password;
42+
}
43+
44+
public void setPassword(String password) {
45+
this.password = password;
46+
}
47+
48+
@ManyToMany
49+
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
50+
public Set<Role> getRoles() {
51+
return roles;
52+
}
53+
54+
public void setRoles(Set<Role> roles) {
55+
this.roles = roles;
56+
}
5957
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.springangularsecurity.service;
22

33
import com.springangularsecurity.model.User;
4+
import com.springangularsecurity.web.UserDto;
45

56
public interface UserService {
6-
void save(User user);
7+
void save(UserDto userDto);
78

8-
User findByUsername(String username);
9+
User findByUsername(String username);
910
}

springangularsecurity/src/main/java/com/springangularsecurity/service/UserServiceImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.springangularsecurity.model.User;
1111
import com.springangularsecurity.repository.RoleRepository;
1212
import com.springangularsecurity.repository.UserRepository;
13+
import com.springangularsecurity.web.UserDto;
1314

1415
@Service
1516
public class UserServiceImpl implements UserService {
@@ -20,9 +21,11 @@ public class UserServiceImpl implements UserService {
2021
@Autowired
2122
private BCryptPasswordEncoder bCryptPasswordEncoder;
2223

23-
public void save(User user) {
24-
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
24+
public void save(UserDto userDto) {
25+
User user = new User();
26+
user.setPassword(bCryptPasswordEncoder.encode(userDto.getPassword()));
2527
user.setRoles(new HashSet<Role>(roleRepository.findAll()));
28+
user.setUsername(userDto.getUsername());
2629
userRepository.save(user);
2730
}
2831

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.springangularsecurity.web;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.ui.Model;
54
import org.springframework.web.bind.annotation.RequestMapping;
65
import org.springframework.web.bind.annotation.RequestMethod;
76
import org.springframework.web.bind.annotation.RestController;
@@ -12,33 +11,30 @@
1211

1312
@RestController
1413
public class UserController {
15-
@Autowired
16-
private UserService userService;
14+
@Autowired
15+
private UserService userService;
16+
17+
@Autowired
18+
private SecurityService securityService;
19+
20+
@RequestMapping(value = "/registration", method = RequestMethod.POST)
21+
public boolean registration(UserDto userDto) {
22+
try {
23+
userService.save(userDto);
24+
securityService.autologin(userDto.getUsername(), userDto.getPassword());
25+
return true;
26+
} catch (Exception e) {
27+
return false;
28+
}
29+
}
30+
31+
@RequestMapping(value = "/login", method = RequestMethod.GET)
32+
public boolean login(UserDto userDto) {
33+
User user = userService.findByUsername(userDto.getUsername());
34+
if (user != null) {
35+
return true;
36+
}
37+
return false;
38+
}
1739

18-
@Autowired
19-
private SecurityService securityService;
20-
21-
@RequestMapping(value = "/registration", method = RequestMethod.POST)
22-
public String registration(User userForm) {
23-
24-
userService.save(userForm);
25-
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
26-
return "success";
27-
}
28-
29-
@RequestMapping(value = "/login", method = RequestMethod.GET)
30-
public String login(Model model, String error, String logout) {
31-
if (error != null)
32-
model.addAttribute("error", "Your username and password is invalid.");
33-
34-
if (logout != null)
35-
model.addAttribute("message", "You have been logged out successfully.");
36-
37-
return "login";
38-
}
39-
40-
/* @RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
41-
public String welcome(Model model) {
42-
return "welcome";
43-
}*/
4440
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.springangularsecurity.web;
2+
3+
public class UserDto {
4+
private String username;
5+
private String password;
6+
7+
public String getUsername() {
8+
return username;
9+
}
10+
11+
public void setUsername(String username) {
12+
this.username = username;
13+
}
14+
15+
public String getPassword() {
16+
return password;
17+
}
18+
19+
public void setPassword(String password) {
20+
this.password = password;
21+
}
22+
23+
}

springangularsecurity/src/main/webapp/app-services/authentication.service.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,10 @@
1717

1818
function Login(username, password, callback) {
1919

20-
/* Dummy authentication for testing, uses $timeout to simulate api call
21-
----------------------------------------------*/
22-
$timeout(function () {
23-
var response;
24-
UserService.GetByUsername(username)
25-
.then(function (user) {
26-
if (user !== null && user.password === password) {
27-
response = { success: true };
28-
} else {
29-
response = { success: false, message: 'Username or password is incorrect' };
30-
}
31-
callback(response);
32-
});
33-
}, 1000);
34-
35-
/* Use this for real authentication
36-
----------------------------------------------*/
37-
//$http.post('/api/authenticate', { username: username, password: password })
38-
// .success(function (response) {
39-
// callback(response);
40-
// });
20+
$http.post('/login', { username: username, password: password })
21+
.success(function (response) {
22+
callback(response);
23+
});
4124

4225
}
4326

springangularsecurity/src/main/webapp/app-services/user.service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
return service;
1616

1717
function GetByUsername(username) {
18-
return $http.get('/api/users/' + username).then(handleSuccess, handleError('Error getting user by username'));
18+
return $http.get('/login' + username).then(handleSuccess, handleError('Error getting user by username'));
1919
}
2020

2121
function Create(user) {
22-
return $http.post('/api/users', user).then(handleSuccess, handleError('Error creating user'));
22+
return $http.post('/registration', user).then(handleSuccess, handleError('Error creating user'));
2323
}
2424

2525
// private functions

springangularsecurity/src/main/webapp/register/register.view.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
<div class="col-md-6 col-md-offset-3">
1+
<div class="col-md-6 col-md-offset-3">
22
<h2>Register</h2>
33
<form name="form" ng-submit="vm.register()" role="form">
4-
<div class="form-group" ng-class="{ 'has-error': form.firstName.$dirty && form.firstName.$error.required }">
5-
<label for="username">First name</label>
6-
<input type="text" name="firstName" id="firstName" class="form-control" ng-model="vm.user.firstName" required />
7-
<span ng-show="form.firstName.$dirty && form.firstName.$error.required" class="help-block">First name is required</span>
8-
</div>
9-
<div class="form-group" ng-class="{ 'has-error': form.lastName.$dirty && form.lastName.$error.required }">
10-
<label for="username">Last name</label>
11-
<input type="text" name="lastName" id="Text1" class="form-control" ng-model="vm.user.lastName" required />
12-
<span ng-show="form.lastName.$dirty && form.lastName.$error.required" class="help-block">Last name is required</span>
13-
</div>
144
<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
155
<label for="username">Username</label>
166
<input type="text" name="username" id="username" class="form-control" ng-model="vm.user.username" required />

0 commit comments

Comments
 (0)