Skip to content

Instantly share code, notes, and snippets.

Choonghee Lee choonghee-lee

Block or report user

Report or block choonghee-lee

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View UserServiceImpl.java
// UserDetailsService를 extends 했을때 오버라이드 해야하는 메소드
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserEntity userEntity = userRepository.findByEmail(email);
if(userEntity == null) {
throw new UsernameNotFoundException(email);
}
return new User(userEntity.getEmail(), userEntity.getEncryptedPassword(), new ArrayList<>());
}
View WebSecurity.java
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter{
private final UserService userDetailsService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
public WebSecurity(UserService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
super();
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
View UserServiceImpl.java
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public UserDto createUser(UserDto user) {
if (userRepository.findByEmail(user.getEmail()) != null) {
throw new RuntimeException("Record already exists");
}
UserEntity userEntity = new UserEntity();
View HelloSecurityApplication.java
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
View pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
View layout.jsp
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!-- 유저가 해당 롤을 가지면 코드를 렌더링한다. -->
<sec:authorize access="hasRole('ADMIN')">
<a class="brand" href="/"> Admins Home </a>
</sec:authorize>
<sec:authorize access="hasRole('USER')">
<a class="brand" href="/"> Users Home </a>
</sec:authorize>
View application.properties
#MVC
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
View pom.xml
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
View Utils.java
@Component
public class Utils {
private final Random RANDOM = new SecureRandom();
private final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public String generateUserId(int length) {
return generateRandomString(length);
}
View UserRepository.java
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
UserEntity findByEmail(String email);
}
You can’t perform that action at this time.