From 608708d239f4de9bb4a83187f6487def5d0ad0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E9=B9=8F?= Date: Fri, 27 Nov 2020 16:56:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B7=BB=E5=8A=A0=E9=9B=B6?= =?UTF-8?q?=E6=97=B6=E7=94=A8=E6=88=B7=20&=20=E6=9D=83=E9=99=90=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scauth/conf/WebSecurityConfig.java | 26 ++++++++++++-- .../scauth/controller/TestAuthController.java | 36 +++++++++++++++++++ .../scauth/handler/AuthLimitHandler.java | 29 +++++++++++++++ .../scauth/handler/LoginSuccessHandler.java | 27 ++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/aiprose/scauth/controller/TestAuthController.java create mode 100644 src/main/java/com/aiprose/scauth/handler/AuthLimitHandler.java create mode 100644 src/main/java/com/aiprose/scauth/handler/LoginSuccessHandler.java diff --git a/src/main/java/com/aiprose/scauth/conf/WebSecurityConfig.java b/src/main/java/com/aiprose/scauth/conf/WebSecurityConfig.java index 5c50d4c..cd433d2 100644 --- a/src/main/java/com/aiprose/scauth/conf/WebSecurityConfig.java +++ b/src/main/java/com/aiprose/scauth/conf/WebSecurityConfig.java @@ -1,9 +1,14 @@ package com.aiprose.scauth.conf; +import com.aiprose.scauth.handler.AuthLimitHandler; +import com.aiprose.scauth.handler.LoginSuccessHandler; import org.springframework.context.annotation.Configuration; +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.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** * @author nelson @@ -13,7 +18,8 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur * @since 1.0 */ @Configuration -public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ +@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true) +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { // super.configure(web); @@ -21,6 +27,22 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { -// super.configure(http); + super.configure(http); + http.formLogin().successHandler(new LoginSuccessHandler()); + + //权限不足 + http.exceptionHandling().accessDeniedHandler(new AuthLimitHandler()); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) + .withUser("nelson").password(new BCryptPasswordEncoder().encode("123456")).roles("admin") + .and() + .withUser("yasaka").password(new BCryptPasswordEncoder().encode("123456")).roles("user") + .and() + .withUser("one").password(new BCryptPasswordEncoder().encode("123456")).roles("gest") + .and() + .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("root"); } } diff --git a/src/main/java/com/aiprose/scauth/controller/TestAuthController.java b/src/main/java/com/aiprose/scauth/controller/TestAuthController.java new file mode 100644 index 0000000..121d49e --- /dev/null +++ b/src/main/java/com/aiprose/scauth/controller/TestAuthController.java @@ -0,0 +1,36 @@ +package com.aiprose.scauth.controller; + +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.security.RolesAllowed; + +/** + * @author nelson + * @desc TODO + * @company 北京中经网软件有限公司 + * @date 2020/11/27 16:42 + * @since 1.0 + */ +@RestController +public class TestAuthController { + + @Secured("ROLE_root") //需要加前缀 + @GetMapping("root") + public String root(){ + return "root"; + } + @PreAuthorize("hasAnyRole('root','admin')") + @GetMapping("admin") + public String gest(){ + return "admin"; + } + + @RolesAllowed("user") + @GetMapping("user") + public String user(){ + return "user"; + } +} diff --git a/src/main/java/com/aiprose/scauth/handler/AuthLimitHandler.java b/src/main/java/com/aiprose/scauth/handler/AuthLimitHandler.java new file mode 100644 index 0000000..8b8f9f9 --- /dev/null +++ b/src/main/java/com/aiprose/scauth/handler/AuthLimitHandler.java @@ -0,0 +1,29 @@ +package com.aiprose.scauth.handler; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.access.AccessDeniedHandler; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author nelson + * @desc 权限不足 + * @company 北京中经网软件有限公司 + * @date 2020/11/27 16:50 + * @since 1.0 + */ +@Slf4j +public class AuthLimitHandler implements AccessDeniedHandler { + + @Override + public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { + log.error("你没有权限访问网址{}",request.getRequestURI()); + response.sendError(403); + } +} diff --git a/src/main/java/com/aiprose/scauth/handler/LoginSuccessHandler.java b/src/main/java/com/aiprose/scauth/handler/LoginSuccessHandler.java new file mode 100644 index 0000000..ffde943 --- /dev/null +++ b/src/main/java/com/aiprose/scauth/handler/LoginSuccessHandler.java @@ -0,0 +1,27 @@ +package com.aiprose.scauth.handler; + +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author nelson + * @desc TODO + * @company 北京中经网软件有限公司 + * @date 2020/11/27 16:15 + * @since 1.0 + */ +public class LoginSuccessHandler implements AuthenticationSuccessHandler { + @Override + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { + System.out.println("login success"); + System.out.println(authentication.getDetails()); + System.out.println(authentication.getAuthorities()); + System.out.println(authentication.getCredentials()); + System.out.println(authentication.getPrincipal()); + } +}