@ -1,4 +1,8 @@ | |||||
package com.cristobalbernal.foro.Respositorios; | package com.cristobalbernal.foro.Respositorios; | ||||
public interface ICategoriaForo { | |||||
import com.cristobalbernal.foro.Entidades.CategoriaForoEntity; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | |||||
public interface ICategoriaForo extends JpaRepository<CategoriaForoEntity,Integer> { | |||||
} | } |
@ -0,0 +1,28 @@ | |||||
package com.cristobalbernal.foro.Servicios; | |||||
import com.cristobalbernal.foro.Entidades.Foro; | |||||
import com.cristobalbernal.foro.Entidades.UsersEntity; | |||||
import com.cristobalbernal.foro.Respositorios.IForo; | |||||
import com.cristobalbernal.foro.Respositorios.IUsers; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.stereotype.Service; | |||||
import java.util.List; | |||||
@Service | |||||
public class ServicioUser { | |||||
@Autowired | |||||
private IUsers iUsers; | |||||
public List<UsersEntity> init(){ | |||||
return iUsers.findAll(); | |||||
} | |||||
/* | |||||
public UsersEntity findById(long id){ | |||||
return iUsers.findById((int) id).orElse(null); | |||||
} | |||||
*/ | |||||
} |
@ -1,44 +0,0 @@ | |||||
package com.cristobalbernal.foro.seguridad; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
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.configurers.LogoutConfigurer; | |||||
import org.springframework.security.core.userdetails.User; | |||||
import org.springframework.security.core.userdetails.UserDetails; | |||||
import org.springframework.security.core.userdetails.UserDetailsService; | |||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | |||||
import org.springframework.security.web.SecurityFilterChain; | |||||
@Configuration | |||||
@EnableWebSecurity | |||||
public class ConfigSeguridad { | |||||
@Bean | |||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | |||||
http | |||||
.authorizeHttpRequests((requests) -> requests | |||||
.requestMatchers("","/home","/","/registrar","/css/**","/image/**","/fonts/**","js/**","/postdetall/{id}").permitAll() | |||||
.anyRequest().authenticated() | |||||
) | |||||
.formLogin((form) -> form | |||||
.loginPage("/login") | |||||
.permitAll() | |||||
) | |||||
.logout(LogoutConfigurer::permitAll); | |||||
return http.build(); | |||||
} | |||||
@Bean | |||||
public UserDetailsService userDetailsService() { | |||||
UserDetails user = | |||||
User.withDefaultPasswordEncoder() | |||||
.username("tobal") | |||||
.password("1234") | |||||
.roles("USER") | |||||
.build(); | |||||
return new InMemoryUserDetailsManager(user); | |||||
} | |||||
} |
@ -0,0 +1,59 @@ | |||||
package com.cristobalbernal.foro.seguridad; | |||||
import com.cristobalbernal.foro.seguridad.models.CustomUserDetailService; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | |||||
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.configurers.LogoutConfigurer; | |||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |||||
import org.springframework.security.crypto.password.PasswordEncoder; | |||||
import org.springframework.security.web.SecurityFilterChain; | |||||
@Configuration | |||||
@EnableWebSecurity | |||||
public class SegurityConfig { | |||||
@Bean | |||||
public CustomUserDetailService userDetailsService(){ | |||||
return new CustomUserDetailService(); | |||||
} | |||||
@Bean | |||||
public PasswordEncoder passwordEncoder() { | |||||
return new BCryptPasswordEncoder(); | |||||
} | |||||
@Bean | |||||
public DaoAuthenticationProvider authenticationProvider() { | |||||
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); | |||||
authProvider.setUserDetailsService(userDetailsService()); | |||||
authProvider.setPasswordEncoder(passwordEncoder()); | |||||
return authProvider; | |||||
} | |||||
@Bean | |||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | |||||
http | |||||
.authorizeHttpRequests() | |||||
.requestMatchers("/crearPregunta","/miperfil").authenticated() | |||||
.anyRequest().permitAll() | |||||
.and() | |||||
.formLogin() | |||||
.usernameParameter("email") | |||||
.loginPage("/login") | |||||
.defaultSuccessUrl("/home") | |||||
.permitAll() | |||||
.and() | |||||
.logout(LogoutConfigurer::permitAll); | |||||
return http.build(); | |||||
} | |||||
} |
@ -0,0 +1,23 @@ | |||||
package com.cristobalbernal.foro.seguridad.models; | |||||
import com.cristobalbernal.foro.Entidades.UsersEntity; | |||||
import com.cristobalbernal.foro.Respositorios.IUsers; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.security.core.userdetails.UserDetails; | |||||
import org.springframework.security.core.userdetails.UserDetailsService; | |||||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | |||||
import java.util.ArrayList; | |||||
public class CustomUserDetailService implements UserDetailsService { | |||||
@Autowired | |||||
private IUsers iUsers; | |||||
@Override | |||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | |||||
UsersEntity users = iUsers.findByEmail(username); | |||||
return new CustomUserDetails(users); | |||||
} | |||||
} |
@ -0,0 +1,53 @@ | |||||
package com.cristobalbernal.foro.seguridad.models; | |||||
import com.cristobalbernal.foro.Entidades.UsersEntity; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.security.core.GrantedAuthority; | |||||
import org.springframework.security.core.userdetails.UserDetails; | |||||
import java.util.Collection; | |||||
public class CustomUserDetails implements UserDetails { | |||||
private final UsersEntity user; | |||||
public CustomUserDetails(UsersEntity user) { | |||||
this.user = user; | |||||
} | |||||
@Override | |||||
public Collection<? extends GrantedAuthority> getAuthorities() { | |||||
return null; | |||||
} | |||||
@Override | |||||
public String getPassword() { | |||||
return user.getPassword(); | |||||
} | |||||
@Override | |||||
public String getUsername() { | |||||
return user.getEmail(); | |||||
} | |||||
@Override | |||||
public boolean isAccountNonExpired() { | |||||
return true; | |||||
} | |||||
@Override | |||||
public boolean isAccountNonLocked() { | |||||
return true; | |||||
} | |||||
@Override | |||||
public boolean isCredentialsNonExpired() { | |||||
return true; | |||||
} | |||||
@Override | |||||
public boolean isEnabled() { | |||||
return true; | |||||
} | |||||
} |