diff --git a/2019-11/spring-23-SS-author/pom.xml b/2019-11/spring-23-SS-author/pom.xml new file mode 100644 index 00000000..156a7590 --- /dev/null +++ b/2019-11/spring-23-SS-author/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + ru.otus + spring-framework-23-security-authorization + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-security + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/Main.java b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..0089f916 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,17 @@ +package ru.otus.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; + +@EnableWebSecurity +@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) +@SpringBootApplication +public class Main { + + public static void main( String[] args ) { + SpringApplication.run( Main.class ); + } + +} diff --git a/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/rest/PagesController.java b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/rest/PagesController.java new file mode 100644 index 00000000..bccb8430 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/rest/PagesController.java @@ -0,0 +1,57 @@ +package ru.otus.spring.rest; + +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import ru.otus.spring.service.MyService; + +@Controller +public class PagesController { + + private final MyService myService; + + public PagesController(MyService myService) { + this.myService = myService; + } + + @GetMapping("/") + public String indexPage() { + return "index"; + } + + @GetMapping("/public") + public String publicPage( ) { + return "public"; + } + + @GetMapping("/user") + public String userPage() { + myService.onlyUser(); + return "user"; + } + + @GetMapping("/admin") + @Secured( "ADMIN" ) + public String adminPage( ) { + myService.onlyUser(); + //myService.onlyAdmin(); + return "admin"; + } + + @GetMapping("/authenticated") + public String authenticatedPage() { + UserDetails userDetails = (UserDetails) SecurityContextHolder + .getContext().getAuthentication().getPrincipal(); + System.out.println(userDetails.getUsername()); + return "authenticated"; + } + + @GetMapping("/success") + public String successPage() { + return "success"; + } +} diff --git a/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/security/SecurityConfiguration.java b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/security/SecurityConfiguration.java new file mode 100644 index 00000000..3989d010 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/security/SecurityConfiguration.java @@ -0,0 +1,76 @@ +package ru.otus.spring.security; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +import java.util.Collection; + +@EnableWebSecurity +@Configuration +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + + + @Override + public void configure(WebSecurity web) { + web.ignoring().antMatchers("/"); + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http.csrf().disable() + //.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) + //.and() + .authorizeRequests().antMatchers("/public").permitAll() + .and() + .authorizeRequests().antMatchers("/authenticated", "/success").authenticated() + .and() + .authorizeRequests().antMatchers("/user").hasAnyRole( "ADMIN", "USER" ) + + .and() + .authorizeRequests().antMatchers("/admin").hasRole( "ADMIN" ) + .and() + .formLogin() + .and() + .logout().logoutUrl("/logout"); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new PasswordEncoder() { + @Override + public String encode(CharSequence charSequence) { + return charSequence.toString(); + } + + @Override + public boolean matches(CharSequence charSequence, String s) { + return charSequence.toString().equals(s); + } + }; + } + + @Autowired + public void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .withUser("admin").password("password").roles("ADMIN") + .and() + .withUser("user").password("password").roles("USER") + .and() + .withUser("manager").password("manager").roles("MANAGER", "USER"); + } +} diff --git a/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/service/MyService.java b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/service/MyService.java new file mode 100644 index 00000000..0c5fef62 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/java/ru/otus/spring/service/MyService.java @@ -0,0 +1,16 @@ +package ru.otus.spring.service; + +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Service; + +@Service +public class MyService { + @PreAuthorize("hasRole('ROLE_USER')") + public String onlyUser() { + return "My love"; + } + + @Secured( "ADMIN" ) + public void onlyAdmin() {} +} diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/admin.html b/2019-11/spring-23-SS-author/src/main/resources/templates/admin.html new file mode 100644 index 00000000..aa1c9563 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/admin.html @@ -0,0 +1,9 @@ + + + + + + +Страница с доступом только админу + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/authenticated.html b/2019-11/spring-23-SS-author/src/main/resources/templates/authenticated.html new file mode 100644 index 00000000..e4756c01 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/authenticated.html @@ -0,0 +1,9 @@ + + + + + + +Только для аторизованных + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/error.html b/2019-11/spring-23-SS-author/src/main/resources/templates/error.html new file mode 100644 index 00000000..f28b51df --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/error.html @@ -0,0 +1,9 @@ + + + + + + +Вам доступ запрещён! + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/index.html b/2019-11/spring-23-SS-author/src/main/resources/templates/index.html new file mode 100644 index 00000000..d4c54ecd --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/index.html @@ -0,0 +1,15 @@ + + + + + + +/public +
+/authenticated +
+/user +
+/admin + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/public.html b/2019-11/spring-23-SS-author/src/main/resources/templates/public.html new file mode 100644 index 00000000..77188469 --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/public.html @@ -0,0 +1,9 @@ + + + + + + +Доступен всем + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/success.html b/2019-11/spring-23-SS-author/src/main/resources/templates/success.html new file mode 100644 index 00000000..4e2a37cd --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/success.html @@ -0,0 +1,9 @@ + + + + + + +Вы успешно вошли ! + + diff --git a/2019-11/spring-23-SS-author/src/main/resources/templates/user.html b/2019-11/spring-23-SS-author/src/main/resources/templates/user.html new file mode 100644 index 00000000..a794bc2d --- /dev/null +++ b/2019-11/spring-23-SS-author/src/main/resources/templates/user.html @@ -0,0 +1,9 @@ + + + + + + +Доступ к USER + +