吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1766|回复: 4
收起左侧

[Java 转载] springboot整合springsecurity快速入门案例

[复制链接]
一蓑烟雨 发表于 2020-11-24 15:56
之前都是ssm,最近尝试重构,使用springboot

要整合springsecurity需要以下步骤

maven依赖
只需要再springboot基础上添加
[XML] 纯文本查看 复制代码
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>


自己写一个service
UserDetailsService 是springsecurity提供的接口,此类就一个方法,主要为了获取用户信息的,相当于用用户名去查数据库,查到的用户信息返给spring,框架给你判断用户密码权限之类的东西,.不用你自己判断.

[Java] 纯文本查看 复制代码
@Service
public class UserDetailsImpl implements UserDetailsService {

    @Autowired
    private UserServiceImpl userService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        // 从数据库中取出用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(s);
        UserInfo user = userService.getUserInfo(userInfo);

// 判断用户是否存在
        if(user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

// 添加权限

            authorities.add(new SimpleGrantedAuthority(user.getPositionId()));


        // 返回UserDetails实现类
        return new User(user.getId(), user.getPassword(), authorities);
    }
}


还需要一个配置类
[Java] 纯文本查看 复制代码
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsImpl userDetailsService;
    @Autowired
    private SuccessLoginHandle successLoginHandle;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                return s.equals(charSequence.toString());
            }
        });
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
// 如果有允许匿名的url,填在下面
// .antMatchers().permitAll()
                .anyRequest().authenticated()
                .and()
// 设置登陆页
                .formLogin().loginPage("/jsp-pages/login")
                //自定义登录路径
                .loginProcessingUrl("/login") // 自定义登录路径
// 设置登陆成功页
                //.defaultSuccessUrl("/")
                 .permitAll()
                .successHandler(successLoginHandle)


// 自定义登陆用户名和密码参数,默认为username和password
 .usernameParameter("id")
// .passwordParameter("password")
                .and()
                .logout().logoutUrl("loginOut");   //自定义退出登录页面;

// 关闭CSRF跨域
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
// 设置拦截忽略文件夹,可以对静态资源放行
        web.ignoring().antMatchers("/css/**", "/js/**","/login/getCode*","/login/askCode*","/img/**","/html/**","/fonts/**");
    }
}


这样就完成了整合,注意点有  要放行静态资源,验证码查询,以及登录页面,不然都拦截完了,用户也没地方登录了.

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

fht000 发表于 2020-11-24 16:01
你这个还是前后不分离的啊,现在基本都分离了
wzj_cqbs 发表于 2020-11-24 16:15
 楼主| 一蓑烟雨 发表于 2020-11-24 16:21
fht000 发表于 2020-11-24 16:01
你这个还是前后不分离的啊,现在基本都分离了

公司很小,项目很小,开发一人,没必要分离,
wanshiz 发表于 2020-11-25 08:02
谢谢楼主,学习一下。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 14:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表