亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決spring security中遇到的問題

 更新時(shí)間:2023年01月20日 14:03:19   作者:成都犀牛  
這篇文章主要介紹了解決spring security中遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

spring security中遇到的問題

1.An Authentication object was not found in the Security Context

在security上下文中沒有找到一個(gè)認(rèn)證對(duì)象,我這邊的問題在于controller中方法添加了認(rèn)證注解,但是配置類中

源自于一片我為了解決攔截靜態(tài)文件的博客,上面說這個(gè)忽視目錄會(huì)以classpath中的static文件夾,實(shí)際上這樣寫有著很大問題,這邊會(huì)讓所有的文件不攔截,雖然你的http認(rèn)證添加了攔截,但是web這個(gè)會(huì)影響到效果,所以一邊允許所有文件不攔截,一邊controller中添加了需要認(rèn)證的注解,所以你一訪問就會(huì)進(jìn)去這個(gè)頁(yè)面,但是進(jìn)去之后發(fā)現(xiàn)在security context并沒有這個(gè)角色值,所以就會(huì)出現(xiàn)這個(gè)異常

最后對(duì)這個(gè)異常說明一句話:這個(gè)普通來看就是越過了普通的往上下文中添加了角色但是進(jìn)去了這個(gè)頁(yè)面就會(huì)出現(xiàn)這個(gè)錯(cuò)誤

2.攔截登錄之后總是會(huì)進(jìn)login?error,而且沒有提示

這個(gè)情況還是有錯(cuò)誤提示才會(huì)好解決問題,在http認(rèn)證配置中的FormLoginConfigurer添加一個(gè)failureUrl("/login/error"),當(dāng)然這個(gè)地址是我們自己定義的,然后對(duì)應(yīng)的congtroller方法:

 @RequestMapping(value = "/login/error")
    public void loginError(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        AuthenticationException exception =
                (AuthenticationException) request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        try {
            response.getWriter().write(exception.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

這樣就可以把最基本的錯(cuò)誤打印出來,然后我們?cè)俑鶕?jù)實(shí)際問題進(jìn)行處理

3.Spring Security BadCredentialsException

這個(gè)具體原因還不是很清楚,但是有個(gè)很簡(jiǎn)單的解決辦法,把所有的密碼加密方式改為相同的,還不可以的話

單獨(dú)寫一個(gè)配置類:

@Configuration
public class BeanConfiguration {

    /**
     * @return Return to the custom password encryptor.
     * The reason why it is extracted separately as a configuration class is because if you don't do this,
     * you cannot achieve unified password encryption, which leads to login failures.
     * Different password encryption results in different passwords.
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new CustomPasswordEncoder();
    }
}

這個(gè)CustomPasswordEncoder類是我自己寫個(gè)一個(gè)密碼加密類,不想使用的話也可以使用官方推薦的:BCryptPasswordEncoder

springboot用security遇到的問題

如果項(xiàng)目中用了 security ,而不用 security 自帶的登入的話 ,會(huì)自動(dòng)跳轉(zhuǎn)其自帶的登入界面(前提是已攔截 放開的可以直接訪問),/login 自帶登入接口路徑 ,/logout 自帶退出接口路勁。

自定義攔截

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyUserDetailsService myUserDetailsService;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**/*.css","/**/*.js","/**/*.jpg","/**/*.gif","/**/*.ico","/**/*.ico","/**/*.woff","/**/*.ttf","/**/*.png").permitAll()
//                .antMatchers("/main").hasAnyRole("ANONYMOUS,USER")
                .anyRequest().authenticated()
                .and().csrf().disable()
                //指定支持基于表單的身份驗(yàn)證。如果未指定FormLoginConfigurer#loginPage(String),則將生成默認(rèn)登錄頁(yè)面
                .formLogin()     //    此開始
                .loginPage("/login")   //security 自帶登入接口
                .permitAll()
                .and()   //此結(jié)束
                .headers().frameOptions().disable()
                .and()
                .rememberMe().tokenValiditySeconds(1209600)
                .and()
                .logout()
                .permitAll();
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論