本文共 4889 字,大约阅读时间需要 16 分钟。
官网https://spring.io/guides/gs/securing-web/
无情的翻译官。。。。。。
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.example securing-web 0.0.1-SNAPSHOT securing-web Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-security org.springframework.security spring-security-test test org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
里面引入了测试要用到的web模块,thymeleaf引擎模块,以及咱们的security模块
测试的项目有两个页面home.html和hello.html
src/main/resources/templates/home.html:
Spring Security Example Welcome!
Click here to see a greeting.
home页面提交到一个/hello请求,会返回一个hello.html:
src/main/resources/templates/hello.html:
Hello World! Hello world!
这个web项目依赖于springmvc,我们可以自定义一下视图解析器:
src/main/java/com/example/securingweb/MvcConfig.java:
package com.example.securingweb;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class MvcConfig 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"); }}
这种无安全机制的web任何人都可以通过localhost:8080/hello 直接访问到hello.html.
假设我们要防止那些没有经过授权的用户进入到hello.html页面,就需要用到我们的security模块。在引入了我们的security包的情况下,我们需要自定义下我们的安全规则;
src/main/java/com/example/securingweb/WebSecurityConfig.java:
package com.example.securingweb;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.provisioning.InMemoryUserDetailsManager;@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Bean @Override public UserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); return new InMemoryUserDetailsManager(user); }}
src/main/resources/templates/login.html:
Spring Security Example Invalid username and password.You have been logged out.
src/main/resources/templates/hello.html:
Hello World! Hello [[${#httpServletRequest.remoteUser}]]!
转载地址:http://facbz.baihongyu.com/