Spring Boot中使用Spring Security如何实现安全控制?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
公司主营业务:成都网站设计、成都网站制作、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出靖远免费做网站回馈大家。
准备工作
首先,构建一个简单的Web工程,以用于后续添加安全控制,也可以用之前Chapter3-1-2 做为基础工程。若对如何使用Spring Boot构建Web应用,可以先阅读 《Spring Boot开发Web应用》 一文。
Web层实现请求映射
@Controller public class HelloController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/hello") public String hello() { return "hello"; } }
实现映射的页面
src/main/resources/templates/index.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security入门</title> </head> <body> <h2>欢迎使用Spring Security!</h2> <p>点击 <a th:href="@{/hello}" rel="external nofollow" >这里</a> 打个招呼吧</p> </body> </html>
src/main/resources/templates/hello.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h2>Hello world!</h2> </body> </html>
可以看到在index.html中提供到 /hello 的链接,显然在这里没有任何安全控制,所以点击链接后就可以直接跳转到hello.html页面。
整合Spring Security
在这一节,我们将对 /hello 页面进行权限控制,必须是授权用户才能访问。当没有权限的用户访问后,跳转到登录页面。
添加依赖
在pom.xml中添加如下配置,引入对Spring Security的依赖。
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ... </dependencies>
Spring Security配置
创建Spring Security的配置类 WebSecurityConfig ,具体如下:
@Configuration @EnableWebSecurity public 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(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").roles("USER"); } }
新增登录请求与页面
在完成了Spring Security配置之后,我们还缺少登录的相关内容。
HelloController中新增 /login 请求映射至 login.html
@Controller public class HelloController { // 省略之前的内容... @RequestMapping("/login") public String login() { return "login"; } }
新增登录页面: src/main/resources/templates/login.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security Example </title> </head> <body> <div th:if="${param.error}"> 用户名或密码错 </div> <div th:if="${param.logout}"> 您已注销成功 </div> <form th:action="@{/login}" method="post"> <div><label> 用户名 : <input type="text" name="username"/> </label></div> <div><label> 密 码 : <input type="password" name="password"/> </label></div> <div><input type="submit" value="登录"/></div> </form> </body> </html>
可以看到,实现了一个简单的通过用户名和密码提交到 /login 的登录方式。
根据配置,Spring Security提供了一个过滤器来拦截请求并验证用户身份。如果用户身份认证失败,页面就重定向到 /login?error ,并且页面中会展现相应的错误信息。若用户想要注销登录,可以通过访问 /login?logout 请求,在完成注销之后,页面展现相应的成功消息。
到这里,我们启用应用,并访问 http://localhost:8080/ ,可以正常访问。但是访问 http://localhost:8080/hello 的时候被重定向到了 http://localhost:8080/login 页面,因为没有登录,用户没有访问权限,通过输入用户名user和密码password进行登录后,跳转到了Hello World页面,再也通过访问 http://localhost:8080/login?logout ,就可以完成注销操作。
为了让整个过程更完成,我们可以修改 hello.html ,让它输出一些内容,并提供“注销”的链接。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h2 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h2> <form th:action="@{/logout}" method="post"> <input type="submit" value="注销"/> </form> </body> </html>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
网站栏目:SpringBoot中使用SpringSecurity如何实现安全控制
文章出自:https://www.cdcxhl.com/article42/pocihc.html
成都网站建设公司_创新互联,为您提供网站建设、网站内链、微信公众号、外贸建站、企业网站制作、做网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联