spring security的简单例子分析

发布时间:2021-11-16 09:24:02 作者:iii
来源:亿速云 阅读:123

这篇文章主要讲解了“spring security的简单例子分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring security的简单例子分析”吧!

1 pom.的主要文件 我引入的thymeleaf-extras-springsecurity5,springboot2.1.6 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

    <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>

    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
2 controller
[@Controller](https://my.oschina.net/u/1774615)

public class KungfuController {

private final String PREFIX = "pages/";

@RequestMapping("/")

public String index() {

	System.out.println("hello word");
	
	return "welcome";
}

@RequestMapping("/userlogin")

public String loginPage() {

	return PREFIX+"login1";
}

@GetMapping("/level1/{path}")

public String level1(@PathVariable("path")String path) {

	return PREFIX+"level1/"+path;
}

@GetMapping("/level2/{path}")

public String level2(@PathVariable("path")String path) {

	return PREFIX+"level2/"+path;
}

@GetMapping("/level3/{path}")

public String level3(@PathVariable("path")String path) {

	return PREFIX+"level3/"+path;
}

}

//配置下

@EnableWebSecurity public class mySecurity extends WebSecurityConfigurerAdapter {

//为啥引入这个bean ,因为在securety在5.0后使用系统的登录模板,默认把密码给加密啦,这个写的是不让密码加密

@Bean
public static NoOpPasswordEncoder passwordEncoder() {

    return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}


protected void configure(HttpSecurity http) throws Exception {

   //定制请求的授权规则
    http.authorizeRequests().antMatchers("/").permitAll()
	
            .antMatchers("/level1/**").hasRole("VIP1")
			
            .antMatchers("/level2/**").hasRole("VIP2")
			
            .antMatchers("/level3/**").hasRole("VIP3");
			
    //开启自动登录的功能
	
 http.formLogin();
 
 //开启自动配置的注销功能
 
 http.logout().logoutSuccessUrl("/");
 
}


public void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.inMemoryAuthentication().withUser("mao").password("123").roles("VIP1","VIP2")
	
            .and().withUser("zhang").password("123").roles("VIP1","VIP3").and()
			
            .withUser("li").password("123").roles("VIP2","VIP3");
			
}

} //该模板都是尚学堂的

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org"

  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h2 align="center">欢迎光临武林秘籍管理系统</h2>

<div sec:authorize="!isAuthenticated()">

<h3 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h3>

</div>

<div sec:authorize="isAuthenticated()">

<h3><span sec:authentication="name"></span>,您好,您的角色有:

	<span sec:authentication="principal.authorities"></span></h3>
	
<form th:action="@{/logout}" method="post">

	<input type="submit" value="注销"/>
</form>

感谢各位的阅读,以上就是“spring security的简单例子分析”的内容了,经过本文的学习后,相信大家对spring security的简单例子分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. spring boot Security 简单使用
  2. Spring Security 简单的用户权限判断

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring security

上一篇:MySQL5.7中怎么优化排序limit

下一篇:ubuntu14.04如何部署ceph 0.87

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》