MErge Conflict
This commit is contained in:
parent
df13ac10c9
commit
affd0a20f8
@ -1,52 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Qualifier("userDetailsServiceImpl")
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder bCryptPasswordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/allCaches").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.defaultSuccessUrl("/allCaches")
|
||||
.permitAll()
|
||||
.and()
|
||||
.logout()
|
||||
.permitAll();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthenticationManager customAuthenticationManager() throws Exception {
|
||||
return authenticationManager();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,11 @@
|
||||
package hhn.labsw.bugageocaching.entities;
|
||||
|
||||
import javax.persistence.*;
|
||||
<<<<<<< HEAD
|
||||
|
||||
import java.util.Set;
|
||||
=======
|
||||
|
||||
import java.util.List;
|
||||
>>>>>>> develop
|
||||
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
@ -21,20 +21,17 @@ public class User {
|
||||
private int rankingPointsSum;
|
||||
private String email;
|
||||
private String password;
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
|
||||
@ManyToMany
|
||||
private List<Role> roles;
|
||||
|
||||
private String token;
|
||||
>>>>>>> develop
|
||||
|
||||
|
||||
@ManyToOne
|
||||
private Team team;
|
||||
|
||||
@ManyToMany
|
||||
Set<Role> roles;
|
||||
|
||||
@Transient
|
||||
private String passwordConfirm;
|
||||
@ -103,22 +100,6 @@ public class User {
|
||||
this.team = team;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
public Set<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Set<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public String getPasswordConfirm() {
|
||||
return passwordConfirm;
|
||||
}
|
||||
|
||||
public void setPasswordConfirm(String passwordConfirm) {
|
||||
this.passwordConfirm = passwordConfirm;
|
||||
=======
|
||||
public List<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
@ -133,6 +114,6 @@ public class User {
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
>>>>>>> develop
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,10 @@
|
||||
package hhn.labsw.bugageocaching.repositories;
|
||||
|
||||
import hhn.labsw.bugageocaching.entities.User;
|
||||
<<<<<<< HEAD
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
User findByUsername(String username);
|
||||
=======
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
@ -19,5 +16,5 @@ public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
|
||||
@Query(value = "SELECT u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true)
|
||||
List<Object[]> getRankingList();
|
||||
>>>>>>> develop
|
||||
|
||||
}
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.service;
|
||||
|
||||
public interface SecurityService {
|
||||
|
||||
String findLoggedInUsername();
|
||||
|
||||
void autoLogin(String username, String password);
|
||||
}
|
||||
@ -1,48 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SecurityServiceImpl implements SecurityService{
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@Qualifier("userDetailsServiceImpl")
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public String findLoggedInUsername() {
|
||||
Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails();
|
||||
if (userDetails instanceof UserDetails) {
|
||||
return ((UserDetails)userDetails).getUsername();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void autoLogin(String username, String password) {
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
||||
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
|
||||
|
||||
authenticationManager.authenticate(usernamePasswordAuthenticationToken);
|
||||
|
||||
if (usernamePasswordAuthenticationToken.isAuthenticated()) {
|
||||
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
|
||||
logger.debug(String.format("Auto login %s successfully!", username));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.service;
|
||||
|
||||
import hhn.labsw.bugageocaching.entities.Role;
|
||||
import hhn.labsw.bugageocaching.entities.User;
|
||||
import hhn.labsw.bugageocaching.repositories.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public UserDetails loadUserByUsername(String username) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user == null) throw new UsernameNotFoundException(username);
|
||||
|
||||
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
||||
for (Role role : user.getRoles()){
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
|
||||
}
|
||||
|
||||
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.service;
|
||||
|
||||
import hhn.labsw.bugageocaching.entities.User;
|
||||
|
||||
public interface UserService {
|
||||
void save(User user);
|
||||
|
||||
User findByUsername(String username);
|
||||
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package hhn.labsw.bugageocaching.service;
|
||||
|
||||
import hhn.labsw.bugageocaching.entities.User;
|
||||
import hhn.labsw.bugageocaching.repositories.RoleRepository;
|
||||
import hhn.labsw.bugageocaching.repositories.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
|
||||
user.setRoles(new HashSet<>(roleRepository.findAll()));
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user