Raa*_*van 3 spring-security spring-boot
我的安全配置为:
package com.vaidiksanatansewa.guruji.security;
import javax.sql.DataSource;
import com.vaidiksanatansewa.guruji.service.UserloginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, IF(status=1, true, false) as enabled from users where username=? and status=1 ")
.authoritiesByUsernameQuery("select users.username as username, user_role.role as role from users inner join user_role on users.role_fid=user_role.id where users.username=? and users.status=1");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and()
.authorizeRequests().antMatchers( "/appointment/**").permitAll()
.antMatchers("/user/**").hasAnyAuthority("ADMIN","USER")
.and()
.csrf().disable();
}
}
Run Code Online (Sandbox Code Playgroud)
和方法在控制器中注释为:
....
@RequestMapping("/user")
@Secured("ROLE_ADMIN")
public List<UserAll> getAll() {
return userService.getAll();
}
@RequestMapping("/user/{id}")
@Secured("ROLE_USER")
public UserAll getById(@PathVariable Long id) {
return userService.getById(id);
}
......
Run Code Online (Sandbox Code Playgroud)
如果没有启用方法授权,一切都可以正常工作,但是当它启用时,我会收到访问被拒绝的错误。似乎所需的一切都已设置,但仍然无法正常工作。你能帮我找出来吗??
感觉很奇怪,但替换@Secured("ROLE_ADMIN")
为@PreAuthorize("hasAuthority('ADMIN')")
确实做到了。我也试过这样做@Secured("hasRole('ROLE_ADMIN')")
,但也没有奏效。嗯,至于现在已经解决了。
归档时间: |
|
查看次数: |
2398 次 |
最近记录: |