package as.lwl.config;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
//负责接收用户名和密码
protected void configure(HttpSecurity httpSecurity) throws Exception {
//开启请求访问的限制 对URL进行授权
httpSecurity.authorizeRequests()
.antMatchers("/login/**").permitAll() //放行login页面所需的CSS图片
.antMatchers("/detail/common/**").hasRole("common") //需要common角色
.antMatchers("/detail/vip/**").hasRole("vip") //需要common角色
.anyRequest().authenticated();
//指定登录页面
httpSecurity.formLogin()
.loginPage("/userLogin").permitAll()
.usernameParameter("name")
.passwordParameter("pwd")
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
rabbitTemplate.convertAndSend("fanout_exchange","",httpServletRequest.getParameter("name")+"用户登录成功!");
}
})
.defaultSuccessUrl("/")
.failureUrl("/userLogin?error")
.failureHandler((req,resp,authentication)->{
rabbitTemplate.convertAndSend("fanout_exchange","",req.getParameter("name")+"用户登录失败!");
});
//注销页面
httpSecurity.logout()
.logoutUrl("/userLogout")
.logoutSuccessUrl("/userLogin");
}
@Autowired
private AmqpAdmin amqpAdmin;
@Autowired
private RabbitTemplate rabbitTemplate;
//验证用户名和密码并分配角色
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//调用密码加密器
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder)
.withUser("123")
.password(bCryptPasswordEncoder.encode("123"))
.roles("common")
.and()
.withUser("456")
.password(bCryptPasswordEncoder.encode("456"))
.roles("vip");
//通过消息服务发送当前用户信息
amqpAdmin.declareExchange(new FanoutExchange("fanout_exchange")); //定义消息模型
amqpAdmin.declareQueue(new Queue("fanout_exchange_sms")); //定义消息队列的名称
amqpAdmin.declareBinding(new Binding("fanout_exchange_sms", //把消息模型,消息队列绑定
Binding.DestinationType.QUEUE,"fanout_exchange",
"",null));
// rabbitTemplate.convertAndSend("fanout_exchange","","123");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
要在Spring Boot中实现用户认证和授权管理,你可以使用Spring Security框架。Spring Security提供了一套强大的功能来处理用户身份验证和授权,以下是一些常见的步骤: 1.添加Spring Security依赖: 在项目的 pom.xml 文件中添加Spring Security依赖。 2.配置Spring Security: 创建一个类并继承,使用@Configuration和@EnableWebSecurity注解标记。重写configure()方法,配置认证和授权规则。 3.使用Spring Security: 添加@EnableGlobalMethodSecurity注解来启用方法级别的访问控制。 4.配置登录页面: 默认情况下,Spring Security提供一个基本的登录页面。你可以自定义登录页面和其他安全相关的页面。 通过以上步骤,你就可以在Spring Boot中实现用户认证和授权管理。根据具体需求,你可以配置认证和授权规则,以及自定义认证方式和登录页面等。
资源推荐
资源详情
资源评论
收起资源包目录
Java课程实验 Spring Security 实现用户认证和授权管理.zip (45个子文件)
Java课程实验 Spring Security 实现用户认证和授权管理
Spring Security 实现用户认证和授权管理
pom.xml 4KB
src
test
java
main
resources
application.properties 0B
templates
detail
vip
2.html 862B
1.html 735B
common
2.html 601B
1.html 669B
index.html 1KB
login
login.html 1KB
static
login
img
login.jpg 12KB
loginError.jpg 774B
css
bootstrap.min.css 141KB
signin.css 906B
java
as
lwl
Security_LWL10.java 312B
controller
ReceiveController.java 634B
SecurityController.java 631B
config
SecurityConfig.java 4KB
.idea
jarRepositories.xml 880B
runConfigurations.xml 346B
workspace.xml 5KB
misc.xml 541B
compiler.xml 709B
.gitignore 184B
encodings.xml 276B
target
classes
application.properties 0B
templates
detail
vip
2.html 862B
1.html 735B
common
2.html 601B
1.html 669B
index.html 1KB
login
login.html 1KB
static
login
img
login.jpg 12KB
loginError.jpg 774B
css
bootstrap.min.css 141KB
signin.css 906B
as
lwl
controller
SecurityController.class 1KB
ReceiveController.class 1KB
Security_LWL10.class 710B
config
SecurityConfig$1.class 2KB
SecurityConfig.class 9KB
LWL_07-1.0-SNAPSHOT.jar 18.24MB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 71B
inputFiles.lst 133B
testCompile
default-testCompile
inputFiles.lst 0B
maven-archiver
pom.properties 89B
generated-sources
annotations
LWL_07-1.0-SNAPSHOT.jar.original 42KB
共 45 条
- 1
资源评论
Meteor.792
- 粉丝: 972
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功