SpringBoot 启动security后 关闭弹出的/login页面

如果没有依赖spring-boot-starter-actuator,只依赖了security,只需要在启动项上排除SecurityAutoConfiguration类即可

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如果依赖了actuator,就需要排除actuator中的ManagementWebSecurityAutoConfiguration。如果有actuator依赖但没有排除这个只排除security,仍然会有/login页面

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class,ManagementWebSecurityAutoConfiguration.class})

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
SpringBoot是一种快速开发的框架,而SpringSecurity是一种用于安全认证和授权的框架。SpringBootSpringSecurity的结合,可以帮助我们快速构建一个安全可靠的应用程序。 使用SpringSecurity需要在pom.xml文件中引入相应的依赖,例如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 在SpringBoot的配置文件中,可以配置一些安全相关的属性,例如: ``` spring.security.user.name=user spring.security.user.password=password spring.security.user.roles=USER ``` 这些属性可以用来配置默认的用户名、密码和角色。当我们访问受保护的资源时,SpringSecurity会自动弹出一个登录框,让用户输入用户名和密码。 在代码中使用SpringSecurity,通常需要定义一个继承自WebSecurityConfigurerAdapter的配置类,例如: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .antMatchers("/**").permitAll() .and().formLogin(); } } ``` 这个配置类中,我们使用了@Autowired注解注入了一个UserDetailsService的实例,用来获取用户信息。在configure方法中,我们配置了访问不同URL需要的角色,以及登录页面的地址。 除此之外,我们还可以自定义一些认证逻辑,例如: ``` @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new CustomUserDetails(user); } } ``` 这个CustomUserDetailsService类实现了UserDetailsService接口,用来从数据库中获取用户信息。在loadUserByUsername方法中,我们可以自定义用户的认证逻辑。 在若依项目中,使用了SpringSecurity来实现用户的登录和授权功能。具体用法可以参考若依项目中的代码,例如: ``` @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsServiceImpl userDetailsService; @Autowired private AuthenticationSuccessHandler authenticationSuccessHandler; @Autowired private AuthenticationFailureHandler authenticationFailureHandler; @Autowired private LogoutSuccessHandler logoutSuccessHandler; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/captchaImage", "/system/user/profile/**").anonymous() .antMatchers("/system/**").hasAnyAuthority("system") .antMatchers("/monitor/**").hasAnyAuthority("monitor") .antMatchers("/tool/**").hasAnyAuthority("tool") .antMatchers("/generator/**").hasAnyAuthority("generator") .antMatchers("/**").authenticated() .and() .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .successHandler(authenticationSuccessHandler) .failureHandler(authenticationFailureHandler) .permitAll() .and() .logout() .logoutUrl("/logout") .logoutSuccessHandler(logoutSuccessHandler) .permitAll() .and() .csrf() .disable() .headers() .frameOptions() .disable() .and() .sessionManagement() .invalidSessionUrl("/login") .maximumSessions(1) .maxSessionsPreventsLogin(true) .expiredSessionStrategy(new CustomExpiredSessionStrategy()) .sessionRegistry(sessionRegistry()); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Bean public SessionRegistry sessionRegistry() { return new SessionRegistryImpl(); } } ``` 在这个配置类中,我们定义了访问不同URL需要的角色,以及登录页面的地址,并实现了自定义的认证逻辑。同时,我们还定义了登录成功、登录失败和退出登录的处理器,以及会话管理相关的配置。 除了配置类之外,若依项目中还使用了一些过滤器和拦截器来实现安全功能。这些过滤器和拦截器的作用和原理可以参考若依项目中的代码。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值