Spring boot Security Custom 화면 설정
2024. 6. 13. 21:52ㆍJava/Spring Boot Security
화면 추가
<input> 태그의 'name' 속성을 기준으로 파라메터 정보를 전달함 'id' 속성은 있어도 작동 안됨
# 디렉토리: /src/main/resources/templates
# home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>
# login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>
# hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello <span th:remove="tag" sec:authentication="name">thymeleaf</span>!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
화면 등록
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
Security Config 설정
WebSecurityConfig 클래스 안의 모든 내용을 지우면, 다시 원래 화면으로 돌아감
정확히는 '.loginPage()'를 지정하지 않으면 됨
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "home").permitAll()
.anyRequest().authenticated())
.formLogin((form) -> form
.loginPage("/login")
.permitAll())
.logout((logout) -> logout.permitAll());
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
// ID: user, PW: password
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
// "password"를 암호화한 값 등록
// 암호화하지 않으면 null 에러 발생함
.password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
/*
비밀번호를 직접 입력할 수 있지만, deprecated되서 사용을 권장하지 않음
@Bean
public UserDetailsService userDetailsService() {
// ID: user, PW: password
UserBuilder users = User.withDefaultPasswordEncoder();
UserDetails user = users
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
*/
}
결과
'Java > Spring Boot Security' 카테고리의 다른 글
Spring boot Security redirect 정보 가져오기 (0) | 2024.07.29 |
---|---|
Spring boot Security redirect after logging (0) | 2024.07.29 |
Spring boot Security에서 sencha 예외 처리 (0) | 2024.07.20 |
Spring boot Security DB 연동 (0) | 2024.06.09 |
Sprng boot Security 적용 (0) | 2024.05.26 |