springboot整合security(二)之配置内存用户

maven所需要的依赖上章已经说过这章不做解释

所谓的内存用户就是将用户账号密码写死在代码中,这种方法适合刚接触security的同学,毕竟学习是需要徐徐渐进的嚒。
好了话不多说直接撸代码

1.新建WebSecurityConfig.java 并继承WebSecurityConfigurerAdapter类

代码如下

package com.hutian.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author hutian
 * @date 2020/7/25 10:11
 */
@EnableWebSecurity //开启security
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    //配置访问策略
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin() //设置默认登陆页面
                .failureForwardUrl("/error") //设置登录失败路由的url
                .defaultSuccessUrl("/success") //设置登录成功的路由url
                .permitAll(); //访问权限所有
        super.configure(http);
    }

    //配置内存用户
    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("zhangsan").password("zspswd").roles("ADMIN")
                .and()
                .withUser("lisi").password("lspswd").roles("USER");
    }

    //密码明文处理
    @SuppressWarnings("deprecation")
    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}

2.登录成功或者失败的controller代码如下

package com.hutian.security;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hutian
 * @date 2020/7/25 10:15
 */
@SpringBootApplication
@RestController
public class Security {
    public static void main(String[] args) {
        SpringApplication.run(Security.class, args);
    }

    @GetMapping("error")
    public String error() {
        String msg = "登录失败===============";
        return msg;
    }

    @GetMapping("success")
    public String success() {
        String msg = "登录成功===============";
        return msg;
    }
}

3.基于内存用户的配置这里已经配置完成,下面实际操作一波

1启动springboot项目,访问localhost:8080
在这里插入图片描述

2输入上面自己配置的账号密码如zhangsan/zspswd然后就会跳转登录成功的页面
在这里插入图片描述

ok基于内存的用户这篇已经讲完了,后续会慢慢更新出基于数据库配置用户权限的文章,谢谢大家

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下配置来跳转到指定的页面: 在 Spring Security 配置中,使用`formLogin()`方法开启表单登录,在其中设置`loginPage()`方法来指定登录页面地址,如下所示: ```java @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 不进行权限验证的 URL .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") // 指定登录页面地址 .permitAll() .and() .logout() .permitAll(); } } ``` 在上述代码中,`/login` 路径为登录页面地址,是一个 GET 请求,访问该路径时返回登录页面。在登录页面中,用户输入用户名和密码,然后提交表单,将会通过 POST 请求发送到`/login`地址。 通过重写 `configure(AuthenticationManagerBuilder auth)` 方法,可以指定用户认证方式默认使用内存中的认证方式。在下面的代码示例中,使用了一对用户名和密码 `user/userpassword`。 ```java @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 不进行权限验证的 URL .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") // 指定登录页面地址 .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user") .password("{noop}userpassword") .roles("USER"); } } ``` 在上述代码中,“{noop}” 是 5.x 版本的特性,用于指定密码以明文形式存储。否则的话,需要使用`PasswordEncoder`接口的实现来加密密码,不过这样做对于一个小型项目并不是必要的。 Spring Security 提供了默认的 login form 表单,不过在以下情况下,可能需要重写默认的登录页面模板。 默认模板位于`/org/springframework/security/web/server/ui/login/LoginPageGeneratingWebFilter.DEFAULT_LOGIN_PAGE_TEMPLATE`。 利用 FreeMarker 模板引擎可以重写该模板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值