赞
踩
2.2.3导入相关的依赖和mysql基本信息.
2.2.4
package com.example.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; 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.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; import javax.annotation.Resource; import javax.sql.DataSource; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true,jsr250Enabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; //用户查询密码 String pwdQuery="select user_name,pwd,available from t_user where user_name=?"; String roleQuery="select u.user_name,r.role_name from t_user u,t_user_role ur,t_role r\n" + " where u.id=ur.user_id and r.id=ur.role_id\n" + " and u.user_name=?"; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(10); //Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder(this.secret); auth.jdbcAuthentication() //.passwordEncoder(NoOpPasswordEncoder.getInstance()) .passwordEncoder(bCryptPasswordEncoder) .dataSource(dataSource) .usersByUsernameQuery(pwdQuery) .authoritiesByUsernameQuery(roleQuery); } @Override public void configure(WebSecurity web) throws Exception { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); } }
2.2.4数据库密码加密信息
2.2.5进行登录测试
2.2.6测试成功
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//允许角色user,admin访问指定路径/user/welcome or /details
.antMatchers("/user/welcome","/user/details").hasAnyRole("USER","ADMIN")
//只允许admin角色访问admin/**路径
.antMatchers("/admin/**").hasAnyAuthority("ROLE_ADMIN")
.anyRequest().permitAll()
.and()
.anonymous()
.and().formLogin().and().httpBasic();
}
@RestController public class HekController { @PreAuthorize(value = "hasAnyRole('ADMIN','USER')") @RequestMapping("/user/welcome") public String getwelcome(){ return "welcome SpringSecurity"; } @PreAuthorize(value = "hasAnyRole('ADMIN','USER')") @RequestMapping("/user/details") public String getdetails(){ return "Hello SpringSecurity"; } @PreAuthorize(value = "hasAuthority('ROLE_ADMIN')") @RequestMapping("/admin/details") public String getadmin(){ return "只有admin权限才能访问此页面"; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。