本文共 5323 字,大约阅读时间需要 17 分钟。
首先创建一个非安全的Web应用
这个应用包含两个页面,一个是home页面,一个是“Hello,World”页面。home页面使用Thymeleaf,相关代码如下:
Spring Security Example Welcome!
Click here to see a greeting.
点击/hello后会跳到hello页面,同样也是用Thymeleaf,相关代码如下:
Hello World! Hello world!
Web应用是基于Spring MVC的,需要配置Spring MVC的视图,用于控制不同的模板。
相关代码如下:
package cn.it1995.demo;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"); }}
addViewController()覆盖了WebMvcConfigurer类中的此方法。这里增加了4个视图控制。
下面是在此基础上增加Spring Security
增加Spring Security相关的Maven
全部的Maven如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.7.RELEASE cn.it1995 demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-security org.springframework.security spring-security-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
新增安全方面的配置,这里只能让唯一的用户进行登录:
package cn.it1995.demo;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); }}
WebSercurityConfig类上带有@EnableWebSecurity注解,让Spring MVC上继承Spring Secrity。这个类需要扩展WebSecurityConfigurerAdapter,重写了2个方法。
configure(HttpSecurity)方法定义了哪些URL需要增加安全,哪些不需要。
userDetailService()方法设置了一个登录用户,用户名为user,密码为password,角色为USER。
这里还需要构造login.html页面:
Spring Security Example Invalid username and password.You have been logged out.
如果认证失败会重定向到/login?error,如果登录成功,点击退出会重定向到/login/logout页面。
这里修改下hello.html的页面,让其显示登录的用户名:
Hello World! Hello [[${#httpServletRequest.remoteUser}]]!
程序运行截图如下:
输入错误的用户名和密码,点击Sign In是登录不进去的。
输入正确的用户名密码
点击here
转载地址:https://it1995.blog.csdn.net/article/details/112213752 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!