捞捞会spring security的佬,有偿
我这边登陆成功后端显示是管理员身份但是还是不能跳转页面
可以借鉴下
实现授权需要对用户的访问进行拦截校验,校验用户的权限是否可以操作指定的资源,Spring Security默认提供授
权实现方法。
在LoginController添加/r/r1或/r/r2
/**
* 测试资源1
* @return
*/
@GetMapping(value = "/r/r1",produces = {"text/plain;charset=UTF-8"})
public String r1(){
return " 访问资源1";
}
/**
* 测试资源2
* @return
*/
@GetMapping(value = "/r/r2",produces = {"text/plain;charset=UTF-8"})
public String r2(){
return " 访问资源2";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
在安全配置类WebSecurityConfifig.java中配置授权规则:
.antMatchers("/r/r1").hasAuthority("p1") .antMatchers("/r/r2").hasAuthority("p2")
1
.antMatchers("/r/r1").hasAuthority(“p1”)表示:访问/r/r1资源的 url需要拥有p1权限。
.antMatchers("/r/r2").hasAuthority(“p2”)表示:访问/r/r2资源的 url需要拥有p2权限。
完整的WebSecurityConfifig方法如下:
//安全拦截机制(最重要)
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/r/r1").hasAuthority("p1")
.antMatchers("/r/r2").hasAuthority("p2")
.antMatchers("/r/**").authenticated()//所有/r/**的请求必须认证通过
.anyRequest().permitAll()//除了/r/**,其它的请求可以访问
.and()
.formLogin()//允许表单登录
.successForwardUrl("/login-success");//自定义登录成功的页面地址
}
可能是controller中使用的路径为login,在没有学配置文件情况下。
//正确使用
@RequestMapping("/hello")
public String login(){
return "登录成功";
}
// 错误的使用
@RequestMapping("/login")
public String errologin(){
return "登录成功";
具体卡在哪个地方?可以将代码贴出来看下
这个问题我们必须搞清楚!一般登录后,服务端会给用户发一个凭证。常见有以下的两种:
基于 Session
客户端会存 cookie
来保存一个 sessionId
,服务端存一个 Session
。
基于 token
客户端存一个 token
串,服务端会在缓存中存一个用来校验此 token
的信息。
对于这个问题,建议先检查一下配置文件中是否配置了正确的权限和页面跳转路径。其次,可以检查一下登录成功后返回的信息是否包含用户身份信息,如果没有,可以考虑在登录成功后手动设置用户身份信息。如果存在身份信息而无法跳转页面,可能是因为跳转路径没有配置正确或者存在权限问题,需要进一步排查。另外,需要确认使用的密码加密方式是否和配置文件中一致,如果不一致,建议使用 DelegatingPasswordEncoder 来兼容多种加密方式。代码示例:
在配置文件中添加权限和页面跳转配置:
spring:
security:
# 配置权限
authorizeRequests:
antMatchers("/admin/**").hasRole("ADMIN")
antMatchers("/user/**").hasRole("USER")
# 配置登录页和登录处理页
formLogin:
loginPage: /login
loginProcessingUrl: /doLogin
# 配置登录成功后的跳转页面
defaultSuccessURL: /index
在登录成功后通过设置用户身份信息:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> authorities = new ArrayList<>();
// 添加用户权限
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
// 构建用户身份信息
UserDetails userDetails = new User("username", "password", authorities);
Authentication newAuth = new UsernamePasswordAuthenticationToken(userDetails, authentication.getCredentials(), userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(newAuth);
在使用密码加密时,建议使用 DelegatingPasswordEncoder,示例代码:
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
对于无法跳转页面的问题,可以尝试在配置文件中添加以下配置:
spring:
security:
# 关闭CSRF保护
csrf:
enabled: false
# 允许FrameOptions显示
headers:
frameOptions:
mode: SAMEORIGIN
以上就是解决 Spring Security 登陆成功后无法跳转页面的几个常见方法,希望对你有所帮助。如果还有问题,欢迎进一步沟通。
以下答案由GPT-4.0Plus大模型与博主A努力中.......共同编写:
根据您的描述,Spring Security登陆成功后无法跳转页面,这通常有以下几个原因:
建议您根据上述可能原因进行检查,常见的解决方案有:
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
那么这个问题有可能是由于权限配置不正确导致的。在 spring security 中,通常会通过配置 WebSecurityConfigurerAdapter
类来完成权限配置,在该类中重写 configure(HttpSecurity http)
方法可以配置相关的权限控制规则。
首先,需要确定自己希望在管理员登录成功后能够访问哪些页面或资源。这些页面或资源被定义为"有权访问的资源"。
其次,需要确定哪些账号具备管理员权限。这些账号被定义为"管理员账号"。
最后,需要在 WebSecurityConfigurerAdapter
类的 configure(HttpSecurity http)
方法中配置相应的权限控制规则,给 "有权访问的资源" 分配 "角色",给 "管理员账号" 分配 "角色",并将 "角色" 和 "权限" 关联。
以下是一个关于如何配置 spring security 初步权限的示例代码:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//配置"/login"路径不需要权限访问
.antMatchers("/login").permitAll()
//配置"有权访问的资源"路径需要"ROLE_USER"角色访问
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
//配置登录页面
.formLogin()
.loginPage("/login")
//配置登录成功跳转页面
.defaultSuccessUrl("/admin/index")
.permitAll()
.and()
.logout()
//配置退出路径和退出后跳转页面
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//配置"用户"和"角色"
auth.inMemoryAuthentication()
.withUser("user").password("{noop}123456").roles("USER")
.and()
.withUser("admin").password("{noop}123456").roles("ADMIN");
}
}
上述代码中,通过 configure(HttpSecurity http)
方法配置了对 "/login" 路径的访问权限和对 "/admin/" 路径的权限控制规则。其中 "/admin/" 路径是 "有权访问的资源"。管理员账号为 "admin",该账号的密码为 "123456",并且该账号被赋予了 "ADMIN" 角色。
如果我的回答解决了您的问题,请采纳!
发代码出来看一看