jetty 添加basic auth
生活随笔
收集整理的這篇文章主要介紹了
jetty 添加basic auth
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
出于安全原因,給jetty加了一個簡單的http basic授權,防止未授權調用,本身服務也是內部調用的,但還是加上了,方式有好幾種,我都試了一遍,記錄一下
第一種 修改web.xml + java 代碼 + 配置文件
加web.xml里面加上這么一段
<security-constraint><web-resource-collection><web-resource-name>api</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><auth-constraint><role-name>admin</role-name></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method><realm-name>Realm</realm-name></login-config><security-role><role-name>admin</role-name></security-role>大概意思就是啟用basic驗證http訪問權限,需要admin角色,url匹配/*,也就是全部,如果有需要這里可以配置多個角色,然后再根據url來分開多個角色對應不同的訪問權限,我這里沒這需求,直接攔截所有的
光上面這段還不行,我的jetty是內嵌使用的,所以直接可以在java代碼里面修改配置
ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) webapp.getSecurityHandler();HashLoginService loginService = new HashLoginService("Realm", "/etc/realm.properties");loginService.setHotReload(true);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);securityHandler.setLoginService(loginService);realm.properties
admin: pwd123,admin格式:用戶名: 密碼, 角色
第二種 修改 java 代碼 + 配置文件
新版的spring mvc可以通過注解,或者代碼的方式啟動一個web項目,而不需要web.xml了,所以上面的方式就不能用了,那么只需要把web.xml里面的代碼以代碼的形式添加到jetty里面就可以了
如下
ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) webapp.getSecurityHandler();HashLoginService loginService = new HashLoginService("Realm", "/etc/realm.properties");loginService.setHotReload(true);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);// 多了這一段,意思的web.xml里面的一樣ConstraintMapping constraintMapping = new ConstraintMapping();Constraint constraint = new Constraint("api", "admin");constraint.setAuthenticate(true);constraintMapping.setConstraint(constraint);constraintMapping.getConstraint().setName("api");constraintMapping.getConstraint().setRoles(new String[]{"admin","admin"});constraintMapping.setPathSpec("/*");securityHandler.addConstraintMapping(constraintMapping);securityHandler.setLoginService(loginService);realm.properties 不變
第三種 只修改 java 代碼
這種方式把配置文件的去掉了,直接在java代碼里面設置用戶名,角色信息,更簡單粗暴,但是不推薦,僅供學習研究
代碼如下
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();HashLoginService loginService = new HashLoginService("Realm");// 代碼的形式添加用戶名密碼final String role = "admin";final String username = "dev";final String password = "dev";IdentityService identityService = loginService.getIdentityService();UserIdentity admin = identityService.newUserIdentity(new Subject(), new MappedLoginService.KnownUser(username, Credential.getCredential(password)), new String[]{role});loginService.getUsers().put(username, admin);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);ConstraintMapping constraintMapping = new ConstraintMapping();Constraint constraint = new Constraint("api", role);constraint.setAuthenticate(true);constraintMapping.setConstraint(constraint);constraintMapping.getConstraint().setName("api");constraintMapping.getConstraint().setRoles(new String[]{role});constraintMapping.setPathSpec("/*");securityHandler.addConstraintMapping(constraintMapping);securityHandler.setLoginService(loginService);springMvcHandler.setSecurityHandler(securityHandler);OK,大概就是這樣,三種方式都在這里,有時間我會把代碼上傳到github上面,方便大家查看
轉載于:https://my.oschina.net/362228416/blog/879831
總結
以上是生活随笔為你收集整理的jetty 添加basic auth的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Springmvc的helloworld
- 下一篇: 基于Vue.js的精选壁纸推荐单页应用