cuốn sách gpt4 ai đã làm

SpringSecurity sử dụng JSON để đăng nhập

In lại Tác giả: Người biết Thời gian cập nhật: 2024-03-13 03:55:20 25 4
mua khóa gpt4 Nike

1.pom.xml

  4.0.0  org.springframework.boot spring -boot-starter-parent 2.6.4    com.yl security-json 0.0.1-SNAPSHOT security-json < description>Dự án demo cho Spring Boot  11    org.springframework.boot spring-boot-starter-security   org.springframework.boot spring- boot-starter-web   org.springframework.boot spring-boot-starter-test test   org.springframework.security< /groupId> spring-security-test test      org.springframework.boot spring-boot-maven-plugin    

2. Bộ lọc xác thực tùy chỉnh

gói com.yl.securityjson.filter; nhập com.fasterxml.jackson.databind.ObjectMapper; nhập org.springframework.http.MediaType; nhập org.springframework.security.authentication.AuthenticationServiceException; nhập org.springframework.security.authentication.UsernamePasswordAuthenticationToken; ;nhập khẩu org.springframework.security.core.Authentication; nhập org.springframework.security.core.AuthenticationException; nhập org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter nhập javax.servlet.http.HttpServletRequest; nhập HttpServletResponse; nhập java.io.IOException; java.util.Map; // Bộ lọc xác thực tùy chỉnh lớp công khai MyAuthenticationFilter mở rộng UsernamePasswordAuthenticationFilter { @Override public Authentication tryAuthentication(HttpServletRequest request, HttpServletResponse reply) ném AuthenticationException { if (!request.getMethod().equals("POST")) { ném mới AuthenticationServiceException("Phương thức xác thực không được hỗ trợ" + request.getMethod()); } if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) { // Khi người dùng truyền tham số ở dạng json String username=null; Mật khẩu chuỗi = null; thử { Map map = new ObjectMapper().readValue(request.getInputStream(),Map.class); tên người dùng = map.get("tên người dùng"); mật khẩu = map.get("password"); ); } if (tên người dùng == null) { tên người dùng = "" } if (mật khẩu == null) { mật khẩu = ""; } tên người dùng = tên người dùng.trim(); UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,password); setDetails(request,authRequest); return this.getAuthenticationManager().authenticate(authRequest); } return super.attemptAuthentication(request, reply);

3. cấu hình bảo mật

gói com.yl.securityjson; nhập com.yl.securityjson.filter.MyAuthenticationFilter; nhập org.springframework.context.annotation.Bean; nhập org.springframework.context.annotation.Configuration; nhập .authentication.builders.AuthenticationManagerBuilder; org.springframework.security.config.annotation.web.builders.HttpSecurity; nhập org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; nhập org.springframework.security.crypto.bcrypt.BCryptPasswordEncoding; nhập .security.crypto.password.PasswordEncoding; org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration public class SecurityConfig mở rộng WebSecurityConfigurerAdapter { //Tiêm bộ lọc xác thực tùy chỉnh @Bean MyAuthenticationFilter AuthenticFilter() ném ngoại lệ { MyAuthenticationFilter filter = new MyAuthenticationFilter(); filter.setAuthenticationManager(authenticationManagerBean()); bộ lọc trả về; } @BeanPasswordEncoding passEncoding() { return new BCryptPasswordEncoding(); } @Override protected void configure(AuthenticationManagerBuilder auth) ném Ngoại lệ { auth.inMemoryAuthentication().withUser("root").password("$2a$10$O8G0X/sUPAA76MV7U3BwY.3Uo8/QMBcqK678Rwkoz.fowbce.CLtO").roles("root"); } @Override protected void configure(HttpSecurity http ) ném ngoại lệ { http.authorizeRequests().anyRequest().authenticated() .and() .formLogin() .loginProcessingUrl("/doLogin") .permitAll() .and().csrf().disable(); //Thay thế chính nó Xác định bộ lọc xác thực và thêm nó vào chuỗi chặn http.addFilterAt(authenticationFilter(), Tên người dùngPasswordAuthenticationFilter.class);

4. bộ điều khiển

gói com.yl.securityjson; nhập org.springframework.web.bind.annotation.GetMapping; nhập org.springframework.web.bind.annotation.RestController; @RestController lớp công khai HelloController { @GetMapping("/hello") chuỗi công khai xin chào () { return "xin chào vue"; } }
25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress