欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

springsecurity如何使用application/json接收數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)springsecurity如何使用application/json接收數(shù)據(jù),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、成都網(wǎng)站制作、山南網(wǎng)絡(luò)推廣、成都小程序開發(fā)、山南網(wǎng)絡(luò)營銷、山南企業(yè)策劃、山南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供山南建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:aaarwkj.com

spring security 使用 application/json 接收數(shù)據(jù)


不了解 security 的請看 security 的簡單使用

https://blog.51cto.com/5013162/2404946


在使用 spring security 登錄用戶的時候 發(fā)現(xiàn)使用 application/josn 后臺不能獲取到數(shù)據(jù)
看 UsernamePasswordAuthenticationFilter 源碼發(fā)現(xiàn)

    //獲取密碼
    protected String obtainPassword(HttpServletRequest request) {
         return request.getParameter(passwordParameter);
    }
    //獲取用戶名
    protected String obtainUsername(HttpServletRequest request) {
         return request.getParameter(usernameParameter);
    }

是直接從request 獲取的  不是從 requestBody 中獲取的

那我們就只需要重寫這兩個方法從 requestBody 中獲取參數(shù)

重寫 UsernamePasswordAuthenticationFilter  類

    public class UserAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

        private ThreadLocal<Map<String,String>> threadLocal = new ThreadLocal<>();

        @Override
        protected String obtainPassword(HttpServletRequest request) {
                String password = this.getBodyParams(request).get(super.SPRING_SECURITY_FORM_PASSWORD_KEY);
                if(!StringUtils.isEmpty(password)){
                        return password;
                }
                return super.obtainPassword(request);
        }

        @Override
        protected String obtainUsername(HttpServletRequest request) {
                String username = this.getBodyParams(request).get(super.SPRING_SECURITY_FORM_USERNAME_KEY);
                if(!StringUtils.isEmpty(username)){
                        return username;
                }
                return super.obtainUsername(request);
        }

        /**
         * 獲取body參數(shù)  body中的參數(shù)只能獲取一次 
         * @param request
         * @return
         */
        private Map<String,String> getBodyParams(HttpServletRequest request){
                Map<String,String> bodyParams =  threadLocal.get();
                if(bodyParams==null) {
                        ObjectMapper objectMapper = new ObjectMapper();
                        try (InputStream is = request.getInputStream()) {
                                bodyParams = objectMapper.readValue(is, Map.class);
                        } catch (IOException e) {
                        }
                        if(bodyParams==null) bodyParams = new HashMap<>();
                        threadLocal.set(bodyParams);
                }

                return bodyParams;
        }
}

自定義 SecurityConfig 類

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
        UserDetailServiceImpl userDetailService;
        @Autowired
        LoginSuccessHandler loginSuccessHandler;

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                //自定義用戶驗證和加密方式
                auth.userDetailsService(userDetailService).passwordEncoder(new BCryptPasswordEncoder());
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
                http.formLogin()                    //  定義當(dāng)需要用戶登錄時候,轉(zhuǎn)到的登錄頁面。
        //          .loginPage("/login.html") //自定義登錄頁面
//                .loginProcessingUrl("/login") //自定義登錄接口地址
//                .successHandler(loginSuccessHandler)
                                .and()
                                // 定義哪些URL需要被保護、哪些不需要被保護
                                .authorizeRequests().antMatchers("/login").permitAll() //不需要保護的URL
                                .anyRequest()               // 任何請求,登錄后可以訪問
                                .authenticated()
                                .and()
                                .logout().logoutSuccessUrl("/login").permitAll() // 登出
                                .and()
                                .csrf().disable();
                //配置自定義過濾器 增加post json 支持
                http.addFilterAt(UserAuthenticationFilterBean(), UsernamePasswordAuthenticationFilter.class);
        }

        private UserAuthenticationFilter UserAuthenticationFilterBean() throws Exception {
                UserAuthenticationFilter userAuthenticationFilter = new UserAuthenticationFilter();
                userAuthenticationFilter.setAuthenticationManager(super.authenticationManager());
                userAuthenticationFilter.setAuthenticationSuccessHandler(loginSuccessHandler);
                return userAuthenticationFilter;
        }
}

登錄成功處理類
LoginSuccessHandler.class

@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

                httpServletResponse.setContentType("application/json;charset=UTF-8");

                httpServletResponse.getWriter().write(authentication.getName());
        }
}

用戶校驗處理類

@Component
public class UserDetailServiceImpl implements UserDetailsService {
        /**
         * 用戶校驗
         * @param s
         * @return
         * @throws UsernameNotFoundException
         */
        @Override
        public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
                Collection<GrantedAuthority> collection = new ArrayList<>();//權(quán)限集合
                String password = new BCryptPasswordEncoder().encode("123456");
                User user = new User(s,password,collection);

                return user;
        }

}

改造完成 支持 post application/json  同時也支持 post form-data/x-www-form-urlencoded
都可以獲取到傳入的參數(shù)

關(guān)于“springsecurity如何使用application/json接收數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

新聞名稱:springsecurity如何使用application/json接收數(shù)據(jù)
當(dāng)前URL:http://aaarwkj.com/article42/pccghc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、微信小程序自適應(yīng)網(wǎng)站、域名注冊虛擬主機、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
欧美国产日韩二区一区在线| av天堂最新资源在线| 亚洲精品一区二区午夜| 国产午夜草莓视频在线观看| 日本不卡视频二区三区| 亚洲人妻av一区二区三区| 欧美日韩国产综合在线观看| 日本欧美一区二区二区视频免费 | 日韩av不卡免费播放| 在线免费观看欧美黄片| 男人天堂av在线资源| 久热伊人精品国产中文| 国产精品专区日产一区| 久久亚洲中文字幕丝袜长腿| 亚洲精品一区二区三区小| 亚洲高清成人综合网站| 闫国产一区二区三区色噜噜| 色婷婷亚洲一区二区三区| 视频免费观看网站不卡| 日韩欧美中文字幕区| 精品三级黄色国产片| 久久婷婷激情亚洲综合色| 2004年亚洲中文字幕| 国产三级精品正在播放| 日韩精品中文字幕免费人妻| 国产成人在线观看av| 伊人久久亚洲精品综合| 中文字幕av久久激情| 丁香六月综合激情啪啪啪| 一区二区三区乱码av| 美女被强到爽高潮不断在线| 中国的性生活黄片免费观看| 国产精品久久久久精品综合| 亚洲精品日韩在线欧美| 欧美日本精品在线观看| 久久女婷五月综合色啪色老板| 亚洲一区二区午夜福利亚洲| 四虎在线永久观看视频| 免费在线一区二区av| 精品一区精品二区国产日韩| 中文字幕亚洲欧美日韩高清 |