首页> 文章> 详情

spring security4认证流程及流程图spring security简单入门

教程分享 > Java教程 (4659) 2024-04-17 12:31:24
Java编程中spring security4认证流程及流程图spring security简单入门
认证流程图:
spring security 认证流程图
sample代码:
package com.leftso.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthenticationExample {
	private static AuthenticationManager am = new SampleAuthenticationManager();

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		while (true) {
			System.out.println("Please enter your username:");
			String name = in.readLine();
			System.out.println("Please enter your password:");
			String password = in.readLine();
			try {
				Authentication request = new UsernamePasswordAuthenticationToken(name, password);
				Authentication result = am.authenticate(request);
				SecurityContextHolder.getContext().setAuthentication(result);
				break;
			} catch (AuthenticationException e) {
				System.out.println("Authentication failed: " + e.getMessage());
			}
		}
		System.out.println("Successfully authenticated. Security context contains: "
				+ SecurityContextHolder.getContext().getAuthentication());
	}
}

class SampleAuthenticationManager implements AuthenticationManager {
	static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();

	static {
		AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
	}

	public Authentication authenticate(Authentication auth) throws AuthenticationException {
		if (auth.getName().equals(auth.getCredentials())) {
			return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
		}
		throw new BadCredentialsException("Bad Credentials");
	}
}

上面的例子,其中认证器只是简单的认证了密码与用户名相同则为合法用户,且设置角色为ROLE_USER


 
https://www.leftso.com/article/127.html

相关文章
Java编程中spring security4是一个spring框架项目的一个安全方面的项目。主要用于用户认证,授权,角色认证
spring security常用注解@Secured、@PreAuthorize 、@PostAuthorize说明,Java编程,spring security
spring boot 入门之security oauth2 jwt完美整合例子,Java编程中spring boot框架+spring security框架+spring security o...
本文主要翻译spring官方的基于spring security框架的oauth2开发指南,spring,oauth2,spring框架,Java编程
java编程中spring框架5.0介绍说明/概述,spring5,spring框架,java编程
Java编程中发邮件也是常用的。但是原生的jdk自带的发送邮件用起来还是比较麻烦的。spring框架在Java语言中完全是神一样的存在,通过spring框架的邮件工具来发送邮件就非常方便了,本文...
java编程为啥会出现spring框架,为什么要有Spring?
Java编程中Spring Boot整合RabbitMQ实现消息中间件RabbitMQ的使用
spring boot 2.0 security 5.0 整合,实现自定义表单登录。spring boot 2.0框架使用。
Java编程中spring boot项目如何获取spring容器applicationContext
Spring Security 配置多个Authentication Providers认证器
Spring框架每个版本的特性及历史介绍,从Spring框架开始1.0到最新的Spring 5.0进行讲解整个Spring生涯中的演变过程以及spring框架生态的扩展。
使用OAuth2安全的Spring REST API,Secure Spring REST API using OAuth2(含demo代码下载)
Spring框架中,可以在6个内置的Scope中创建bean,也可以定义自定义范围。 在这六个范围中,只有在使用Web感知的ApplicationContext时才有四个范围可用。singlet...