-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from aistoica/Authentication
[Authentication] Added basic http authenticatio non all endpoints
- Loading branch information
Showing
42 changed files
with
851 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/springframework/samples/petclinic/model/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.springframework.samples.petclinic.model; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
import javax.persistence.UniqueConstraint; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
@Entity | ||
@Table(name = "roles" ,uniqueConstraints = @UniqueConstraint(columnNames = {"username", "role"})) | ||
public class Role extends BaseEntity { | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "username") | ||
@JsonIgnore | ||
private User user; | ||
|
||
@Column( name = "role") | ||
private String name; | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/org/springframework/samples/petclinic/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.springframework.samples.petclinic.model; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
import javax.persistence.Table; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
@Entity | ||
@Table(name = "users") | ||
public class User { | ||
|
||
@Id | ||
@Column(name = "username") | ||
private String username; | ||
|
||
@Column(name = "password") | ||
private String password; | ||
|
||
@Column(name = "enabled") | ||
private Boolean enabled; | ||
|
||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER) | ||
private Set<Role> roles; | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public Boolean getEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(Boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public Set<Role> getRoles() { | ||
return roles; | ||
} | ||
|
||
public void setRoles(Set<Role> roles) { | ||
this.roles = roles; | ||
} | ||
|
||
@JsonIgnore | ||
public void addRole(String roleName) { | ||
if(this.roles == null) { | ||
this.roles = new HashSet<>(); | ||
} | ||
Role role = new Role(); | ||
role.setName(roleName); | ||
this.roles.add(role); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.springframework.samples.petclinic.repository; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.samples.petclinic.model.User; | ||
|
||
public interface UserRepository { | ||
|
||
void save(User user) throws DataAccessException; | ||
} |
68 changes: 68 additions & 0 deletions
68
...in/java/org/springframework/samples/petclinic/repository/jdbc/JdbcUserRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.springframework.samples.petclinic.repository.jdbc; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.dao.EmptyResultDataAccessException; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
import org.springframework.samples.petclinic.model.Role; | ||
import org.springframework.samples.petclinic.model.User; | ||
import org.springframework.samples.petclinic.repository.UserRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@Profile("jdbc") | ||
public class JdbcUserRepositoryImpl implements UserRepository { | ||
|
||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; | ||
private SimpleJdbcInsert insertUser; | ||
|
||
@Autowired | ||
public JdbcUserRepositoryImpl(DataSource dataSource) { | ||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); | ||
this.insertUser = new SimpleJdbcInsert(dataSource).withTableName("users"); | ||
} | ||
|
||
@Override | ||
public void save(User user) throws DataAccessException { | ||
|
||
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user); | ||
|
||
try { | ||
getByUsername(user.getUsername()); | ||
this.namedParameterJdbcTemplate.update("UPDATE users SET password=:password, enabled=:enabled WHERE username=:username", parameterSource); | ||
} catch (EmptyResultDataAccessException e) { | ||
this.insertUser.execute(parameterSource); | ||
} finally { | ||
updateUserRoles(user); | ||
} | ||
} | ||
|
||
private User getByUsername(String username) { | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
params.put("username", username); | ||
return this.namedParameterJdbcTemplate.queryForObject("SELECT * FROM users WHERE username=:username", | ||
params, BeanPropertyRowMapper.newInstance(User.class)); | ||
} | ||
|
||
private void updateUserRoles(User user) { | ||
Map<String, Object> params = new HashMap<>(); | ||
params.put("username", user.getUsername()); | ||
this.namedParameterJdbcTemplate.update("DELETE FROM roles WHERE username=:username", params); | ||
for (Role role : user.getRoles()) { | ||
params.put("role", role.getName()); | ||
if (role.getName() != null) { | ||
this.namedParameterJdbcTemplate.update("INSERT INTO roles(username, role) VALUES (:username, :role)", params); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...main/java/org/springframework/samples/petclinic/repository/jpa/JpaUserRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.springframework.samples.petclinic.repository.jpa; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.samples.petclinic.model.User; | ||
import org.springframework.samples.petclinic.repository.UserRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@Profile("jpa") | ||
public class JpaUserRepositoryImpl implements UserRepository { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
@Override | ||
public void save(User user) throws DataAccessException { | ||
if (this.em.find(User.class, user.getUsername()) == null) { | ||
this.em.persist(user); | ||
} else { | ||
this.em.merge(user); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../springframework/samples/petclinic/repository/springdatajpa/SpringDataUserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.springframework.samples.petclinic.repository.springdatajpa; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.repository.Repository; | ||
import org.springframework.samples.petclinic.model.User; | ||
import org.springframework.samples.petclinic.repository.UserRepository; | ||
|
||
@Profile("spring-data-jpa") | ||
public interface SpringDataUserRepository extends UserRepository, Repository<User, Integer> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.