html页面访问安全控制,学习spring security(一 ) demo

1.跟着spring官方给的guide做个demo,来学习spring security.

首先引入依赖:

dependencies {

compile('org.springframework.boot:spring-boot-starter-web')

compile('org.springframework.boot:spring-boot-starter-thymeleaf')

compile('org.springframework.cloud:spring-cloud-starter-security')

testCompile group: 'junit', name: 'junit', version: '4.12'

}

2.配置springmvc

package com.test.security;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration

public 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");

}

}

3.配置spring security

package com.test.security;

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

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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);

}

}

以上配置做了如下工作

除了“/”,”/home”(首页),”/login”(登录),”/logout”(注销),之外,其他路径都需要认证,以上4个path是允许所有人所有角色访问。

指定“/login”该路径为登录页面,当未认证的用户尝试访问任何受保护的资源时,都会跳转到“/login”,比如如果用户访问下面的hello页面时,也会跳转到/login.

默认指定“/logout”为注销页面

配置一个内存中的用户认证器,使用user/password作为用户名和密码,具有USER角色

4.项目的启动点

package com.test.security;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class application {

public static void main(String[] args) {

SpringApplication.run(application.class, args);

}

}

5.接下来是要访问的页面,其中home.html,login.html是不受安全控制的页面;

而hello.html页面是受安全控制的。

home.html  主要是欢迎页面,并有个跳转链接到hello页面,但是hello页面需要安全验证

Spring Security Example

Welcome!

Click here to see a greeting.

hello.html

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

Hello World!

Hello [[${#httpServletRequest.remoteUser}]]!

login.html

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

Spring Security Example

Invalid username and password.

You have been logged out.

User Name :
Password:

运行后运行结果如下:

登录http://localhost:8080/ 跳转到home页面

28ecd6e0e3074f7fe80747d8323947f4.png

然后点击here要去往hello页面,点击here后跳转到以下要输入用户名和密码的页面,因为要访问hello页面是需要权限的。

16e2f6535408a12c36a135ba3f941a59.png

此时输入一个错误的用户名和密码,

c2023c3bd9b4edce3572d20fff25d885.png

点击登录,会爆出错误的信息如下:

04bd450037ded62d99a4131fd3ef02eb.png

如果输入正确的,用户名和密码,以上在SecurityConfiguration类中设置的user password,会出现以下页面:

4da71f11de94beb8a309b6b2e00c2d72.png

然后点击sign out

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值