SpringSecurity简单使用

maven

自定义登录逻辑

package com.leng.springsecurity.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
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.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 自定义登录逻辑
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    //没有连数据库就自己定义一个密码出来
    @Autowired
    private PasswordEncoder pw;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行了loadUserByUsername方法");
        //1.查询数据库判断用户名是否存在,如果不存在就会抛出异常UsernameNotFoundException
        if(!"admin".equalsIgnoreCase(username)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //2.把查询出来的密码(在存入数据库前已加密)进行解析或者直接把密码存入构造方法
        String encode = pw.encode("123");
        //参数1:用户名,参数2:密码,参数3:权限
        return new User(username,encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}

自定义登录页面及失败页面

创建一个SecurityConfig的配置类,当做SpringSecurity配置类

        这里有两种返回方式

                1.successForwardUrl/failureForwardUrl:正常成功下的登录成功和登录失败返回

                2.successHandler/failureHandler:自定义的返回配置,用于前后端分离/跳转外部链接

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /**
    * Description: 授权
    * date: 2023/1/15 21:10
    * 
    * @author: AL
    * @since JDK 9
    */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //表单提交
        http.formLogin()
                //当请求是/login时代表是登录,必须和表单提交的地址一样,去执行UserDetailsServiceImpl
                .loginProcessingUrl("/login")
                //自定义登录页面
                .loginPage("/login.html")
                //登录成功后跳转页面,POST的请求
                //.successForwardUrl("/toMain")
                //登录成功后的处理器,不能和successForwardUrl共存(用于前后端分离/跳转外部链接)
                .successHandler(new MyAuthenticationSuccessHandler("http://www.baidu.com"))
                //登录失败后跳转页面,POST的请求
                //.failureForwardUrl("/toError")
                //登录失败后的处理器,不能和failureForwardUrl共存(用于前后端分离/跳转外部链接)
                .failureHandler(new MyAuthenticationFailureHandler("/error.html"));

        //手动授权认证
        http.authorizeRequests()
                .antMatchers("/error.html").permitAll()
                //login.html不需要认证
                .antMatchers("/login.html").permitAll()
                //所有请求必须被认证,必须要登录后执行
                .anyRequest().authenticated();
        //关闭csrf防护,可以简单理解为是一个防火墙
        http.csrf().disable();
    }

    //代表一个实例
    @Bean
    public PasswordEncoder getPW() {
        return new BCryptPasswordEncoder();
    }
}

自定义登录成功处理器

package com.leng.springsecurity.handle;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Description:  登录成功处理器
* date: 2023/1/18 9:17
* 
* @author: AL
* @since JDK 9
*/
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private String url;

    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        User user= (User) authentication.getPrincipal();
        System.out.println(user.getUsername());
        //出于安全原因密码不回打印出来,为null
        System.out.println(user.getPassword());
        //权限 
        System.out.println(user.getAuthorities());
        httpServletResponse.sendRedirect(url);
    }
}

自定义失败处理器

package com.leng.springsecurity.handle;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: 登录失败处理器
 * date: 2023/1/18 9:16
 *
 * @author: AL
 * @since JDK 9
 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    private String url;

    public MyAuthenticationFailureHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.sendRedirect(url);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 是一个功能强大且灵活的框架,用于在 Java 应用程序中实现身份验证和授权。以下是一个简单的示例,演示了如何在 Spring Security 中进行基本配置和使用。 首先,在 pom.xml 文件中添加 Spring Security 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 接下来,创建一个配置类来配置 Spring Security。可以创建一个类,并使用 `@EnableWebSecurity` 注解进行标记: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public").permitAll() .anyRequest().authenticated() .and() .formLogin().permitAll() .and() .logout().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password(passwordEncoder().encode("password")).roles("ADMIN") .and() .withUser("user").password(passwordEncoder().encode("password")).roles("USER"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 上述配置类中,我们通过 `configure(HttpSecurity http)` 方法定义了 URL 的访问权限规则,并配置了登录和登出行为。通过 `configure(AuthenticationManagerBuilder auth)` 方法,我们在内存中定义了两个用户(admin 和 user)及其密码和角色。 最后,创建一个简单的控制器类来定义一些访问路径。可以使用 `@RestController` 注解来标记该类,并使用 `@RequestMapping` 注解来定义请求路径: ```java @RestController public class HelloController { @RequestMapping("/public") public String publicEndpoint() { return "Public endpoint"; } @RequestMapping("/private") public String privateEndpoint() { return "Private endpoint"; } } ``` 以上示例中,`/public` 路径是公开访问的,而 `/private` 路径需要进行身份验证才能访问。 这只是一个简单的示例,展示了 Spring Security 的基本用法。你可以根据自己的需求进行更复杂的配置和定制化。希望对你有所帮助!如果有更多问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值